<?php
/**
 * SharingOption.php.
 *
 * @package RewardsWP
 */

namespace Awesomemotive\Rewardswp\Settings\Options\Referrals;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Awesomemotive\Rewardswp\Abstracts\AbstractOption;
use Awesomemotive\Rewardswp\Interfaces\OptionInterface;

/**
 * Class SharingOption.
 */
class SharingOption extends AbstractOption implements OptionInterface {
	/**
	 * Get the setting key used in the option array.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'sharing';
	}

	/**
	 * Get the setting group.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_group(): string {
		return 'referrals';
	}

	/**
	 * Get the default value for the setting.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_default_value(): array {
		return [
			'x'             => true,
			'x_message'     => esc_html__( 'Visit {site_name} to receive your free gift!', 'rewardswp' ),
			'facebook'      => true,
			'email'         => true,
			'email_subject' => esc_html__( 'A little gift for you!', 'rewardswp' ),
			'email_message' => esc_html__( 'Wanted to share a little gift with you. Get a {reward_name} with your first purchase at {site_name}.', 'rewardswp' )
				. PHP_EOL . PHP_EOL .
				esc_html__( 'Just click the following link to claim it:', 'rewardswp' )
				. PHP_EOL .
				'{referral_link}',
		];
	}

	/**
	 * Get the label for the setting.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_label(): string {
		return esc_html__( 'Sharing Options', 'rewardswp' );
	}

	/**
	 * Get the type of the setting (text, textarea, select, etc).
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_type(): string {
		return 'sharing_ui';
	}

	/**
	 * Custom sanitization.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed $value The value to be sanitized.
	 *
	 * @return array
	 */
	public function sanitize( $value ): array {
		$default = $this->get_default_value();

		return [
			'x'             => ! empty( $value['x'] ),
			'facebook'      => ! empty( $value['facebook'] ),
			'email'         => ! empty( $value['email'] ),
			'x_message'     => empty( $value['x_message'] )
				? $default['x_message']
				: sanitize_text_field( $value['x_message'] ),
			'email_subject' => empty( $value['email_subject'] )
				? $default['email_subject']
				: sanitize_text_field( $value['email_subject'] ),
			'email_message' => empty( $value['email_message'] )
				? $default['email_message']
				: sanitize_textarea_field( $value['email_message'] ),
		];
	}

	/**
	 * Retrieve the active sharing options.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_active_options(): array {
		$enabled_sharing_options = [];

		foreach ( $this->get_value() as $key => $value ) {
			if ( true === $value ) {
				$enabled_sharing_options[] = $key;
			}
		}

		return $enabled_sharing_options;
	}
}
