<?php

namespace WPML;

use WPML\Collect\Support\Traits\Macroable;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Obj;
use WPML\FP\Relation;
use function WPML\FP\curryN;

class UIPage {

	const TM_PAGE = 'tm/menu/main.php';

	use Macroable;

	public static function init() {
		self::macro( 'isPage', Relation::propEq( 'page' ) );

		self::macro( 'isLanguages', self::isPage( WPML_PLUGIN_FOLDER . '/menu/languages.php' ) );

		self::macro( 'isTranslationManagement', self::isPage( self::TM_PAGE ) );

		self::macro( 'isTMDashboard', Logic::both(
			self::isTranslationManagement(),
			Logic::anyPass( [ Relation::propEq( 'sm', null ), Relation::propEq( 'sm', 'dashboard' ) ] )
		) );

		self::macro( 'isTMBasket', Logic::both( self::isTranslationManagement(), Relation::propEq( 'sm', 'basket' ) ) );

		self::macro( 'isTMJobs', Logic::both( self::isTranslationManagement(), Relation::propEq( 'sm', 'jobs' ) ) );

		self::macro( 'isTMTranslators', Logic::both( self::isTranslationManagement(), Relation::propEq( 'sm', 'translators' ) ) );

		self::macro( 'isTMATE', Logic::both( self::isTranslationManagement(), Relation::propEq( 'sm', 'ate-ams' ) ) );

		self::macro( 'isTroubleshooting', self::isPage( WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' ) );

		self::macro( 'isTranslationQueue', self::isPage( 'tm/menu/translations-queue.php' ) );

		self::macro( 'getTM', Fns::always( 'admin.php?page=' . self::TM_PAGE ) );

		self::macro( 'getTMDashboard', Fns::always( self::getTM() . '&sm=dashboard' ) );

		self::macro( 'getTMBasket', Fns::always( self::getTM() . '&sm=basket' ) );

		self::macro( 'getTMATE', Fns::always( self::getTM() . '&sm=ate-ams' ) );

		self::macro( 'getTMTranslators', Fns::always( self::getTM() . '&sm=translators' ) );

		self::macro( 'getTMJobs', Fns::always( self::getTM() . '&sm=jobs' ) );

		self::macro( 'getLanguages', Fns::always( 'admin.php?page=' . WPML_PLUGIN_FOLDER . '/menu/languages.php' ) );

		self::macro( 'getTranslationQueue', Fns::always( 'admin.php?page=tm/menu/translations-queue.php' ) );

		self::macro( 'getTroubleshooting', Fns::always( 'admin.php?page=' . WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' ) );

	}

	public static function isSettings( $get = null ) {
		$isSettings = function ( $get ) {
			return defined( 'WPML_TM_FOLDER' )
				? self::isPage( WPML_TM_FOLDER . '/menu/settings', $get )
				: self::isPage( WPML_PLUGIN_FOLDER . '/menu/translation-options.php', $get );
		};

		return call_user_func_array( curryN( 1, $isSettings ), func_get_args() );
	}

	public static function isMainSettingsTab( $get = null ) {
		return self::isSettingTab( 'mcsetup', $get );
	}

	public static function isNotificationSettingsTab( $get = null ) {
		return self::isSettingTab( 'notifications', $get );
	}

	public static function isCustomXMLConfigSettingsTab( $get = null ) {
		return self::isSettingTab( 'custom-xml-config', $get );
	}


	public static function isSettingTab( $tab = null, $get = null ) {
		$fn = function ( $tab, $get ) {
			if ( self::isSettings( $get ) ) {
				return Obj::propOr( 'mcsetup', 'sm', $get ) === $tab;
			}

			return false;
		};

		return call_user_func_array( curryN( 2, $fn ), func_get_args() );
	}

	public static function getSettings() {
		$page = defined( 'WPML_TM_FOLDER' ) ? WPML_TM_FOLDER . '/menu/settings' : WPML_PLUGIN_FOLDER . '/menu/translation-options.php';
		return 'admin.php?page=' . $page;
	}

}

UIPage::init();

