<?php

namespace Uncanny_Automator\Integrations\Pretty_Links;

/**
 * Class PRLI_ANON_CREATE_A_LINK
 *
 * @package Uncanny_Automator
 *
 * @method \Uncanny_Automator\Integrations\Pretty_Links\Pretty_Links_Helpers get_item_helpers()
 */
class PRLI_ANON_CREATE_A_LINK extends \Uncanny_Automator\Recipe\Trigger {

	/**
	 * Declare the trigger's identity and monitored hook for the code-defined loading path.
	 *
	 * @return \Uncanny_Automator\Recipe\Trigger_Definition
	 */
	public static function definition() {
		return self::new_definition( 'PRLI_ANON_CREATE_LINK', 'PRETTY_LINKS' )
			->trigger_type( 'anonymous' )
			->trigger_meta( 'PRLI_REDIRECTION' )
			->hook( 'prli-create-link', 10, 2 );
	}

	/**
	 * Set up the trigger's sentences.
	 *
	 * @return void
	 */
	protected function setup_trigger() {
		// Trigger sentence - Pretty Links
		// translators: 1: Redirection type
		$this->set_sentence( sprintf( esc_attr_x( 'A pretty link of {{a specific redirect type:%1$s}} is created', 'Pretty Links', 'uncanny-automator' ), $this->get_trigger_meta() ) );
		$this->set_readable_sentence( esc_attr_x( 'A pretty link of {{a specific redirect type}} is created', 'Pretty Links', 'uncanny-automator' ) );
	}

	/**
	 * Define the trigger's option fields.
	 *
	 * @return array[]
	 */
	public function options() {
		return array(
			Automator()->helpers->recipe->field->select_field_args(
				array(
					'input_type'      => 'select',
					'option_code'     => $this->get_trigger_meta(),
					'label'           => esc_html_x( 'Redirection type', 'Pretty Links', 'uncanny-automator' ),
					'required'        => true,
					'options'         => array(),
					'remote_data'     => $this->get_item_helpers()->remote_data_load_config( 'redirection_types_for_triggers' ),
					'options_show_id' => false,
					'relevant_tokens' => array(),
				)
			),
		);
	}

	/**
	 * Validate the trigger against the incoming hook arguments.
	 *
	 * @param array $trigger   The trigger definition and selected options.
	 * @param array $hook_args The prli-create-link hook arguments: link ID and link row.
	 *
	 * @return bool True when the created link's redirect type matches the trigger's selection.
	 */
	public function validate( $trigger, $hook_args ) {
		if ( ! isset( $trigger['meta'][ $this->get_trigger_meta() ] ) ) {
			return false;
		}

		list( $id, $args ) = $hook_args;

		// Fail the trigger if incoming redirect type from action hook is not set.
		if ( ! isset( $args['redirect_type'] ) ) {
			return false;
		}

		$selected_redirection_type = $trigger['meta'][ $this->get_trigger_meta() ];
		// Any redirection type
		if ( intval( '-1' ) === intval( $selected_redirection_type ) ) {
			return true;
		}

		if ( absint( $args['redirect_type'] ) === absint( $selected_redirection_type ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Define the tokens exposed by the trigger.
	 *
	 * @param array $trigger The trigger definition and selected options.
	 * @param array $tokens  The existing tokens.
	 *
	 * @return array The tokens merged with the common created-link tokens.
	 */
	public function define_tokens( $trigger, $tokens ) {
		$prli_tokens = $this->get_item_helpers()->prli_common_tokens_for_link_created();

		return array_merge( $tokens, $prli_tokens );
	}

	/**
	 * Populate the tokens with actual values when the trigger runs.
	 *
	 * @param array $trigger   The trigger definition and selected options.
	 * @param array $hook_args The prli-create-link hook arguments: link ID and link row.
	 *
	 * @return array The hydrated token values keyed by token ID.
	 */
	public function hydrate_tokens( $trigger, $hook_args ) {
		$parse_token_values = array();

		if ( ! empty( $hook_args ) ) {
			// Hydrate Pretty Links tokens.
			$parse_token_values = $this->get_item_helpers()->hydrate_prli_common_tokens( $hook_args );
		}

		return $parse_token_values;
	}
}
