<?php
/**
 * @package Polylang-Pro
 */

namespace WP_Syntex\Polylang_Pro\Modules\Machine_Translation\Posts;

defined( 'ABSPATH' ) || exit;

use PLL_Base;
use PLL_Toggle_User_Meta;
use PLL_Toggle_User_Button_REST;
use WP_Syntex\Polylang_Pro\Modules\Machine_Translation\Services\Service_Interface;

/**
 * Adds a button in the languages metabox (block editor), allowing to create a new translation by using a machine
 * translation service.
 * Exposes the pll_machine_translation user meta in the REST API.
 *
 * @since 3.6
 */
class Button_REST extends PLL_Toggle_User_Button_REST {

	/**
	 * Instance of the machine translation service.
	 *
	 * @var Service_Interface
	 */
	private $service;

	/**
	 * Constructor.
	 *
	 * @since 3.6
	 *
	 * @param PLL_Base          $polylang Polylang object. Unused but passed by `PLL_Admin_Loader` anyway...
	 * @param Service_Interface $service  Machine translation service.
	 */
	public function __construct( PLL_Base $polylang, Service_Interface $service ) {
		$this->service = $service;

		parent::__construct( $polylang->model, new PLL_Toggle_User_Meta( sprintf( 'pll_machine_translation_%s', $this->service->get_slug() ) ) );

		add_filter( 'pll_block_editor_plugin_settings', array( $this, 'get_service_settings' ) );
		add_filter( 'pll_block_editor_plugin_settings', array( $this, 'get_settings_errors' ) );
	}

	/**
	 * Adds service properties in UI settings.
	 *
	 * @since 3.6
	 *
	 * @param array $settings UI settings.
	 * @return array Updated UI settings.
	 */
	public function get_service_settings( $settings ) {
		$settings['machine_translation'] = array(
			'slug'     => $this->service::get_slug(),
			'name'     => $this->service->get_name(),
			'icon'     => $this->service->get_icon_properties(),
			'isActive' => $this->service->is_active(),
		);
		return $settings;
	}

	/**
	 * Adds machine translation errors in UI settings.
	 *
	 * @since 3.6
	 *
	 * @param array $settings UI settings.
	 * @return array Updated UI settings.
	 */
	public function get_settings_errors( $settings ) {
		$settings_errors = get_settings_errors( 'polylang' );

		if ( empty( $settings_errors ) ) {
			return $settings;
		}

		$settings['machine_translation']['errors'] = $settings_errors;

		return $settings;
	}

	/**
	 * Returns the description of the schema for the current user meta.
	 *
	 * @since 3.8
	 *
	 * @return string The description of the schema.
	 */
	protected function get_schema_description(): string {
		return __( 'Whether the machine translation feature is enabled or not per post type for the current user.', 'polylang-pro' );
	}
}
