<?php

/**
 * WooCommerce Cart & Checkout Blocks integration.
 *
 * Registers the front-end/editor script + style with the Cart and Checkout
 * blocks so a gift-wrapping control can be rendered into the order summary
 * (via the ExperimentalOrderMeta slot). The live wrap state and fee travel
 * over the Store API; see CGWFW_Plugin::register_blocks_integration() for the
 * endpoint-data and update-callback registration.
 *
 * @package CartGiftWrapping
 */
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
/**
 * Class CGWFW_Blocks_Integration
 */
class CGWFW_Blocks_Integration implements IntegrationInterface {
    /**
     * Settings helper.
     *
     * @var CGWFW_Settings
     */
    private $settings;

    /**
     * Cart handler.
     *
     * @var CGWFW_Cart
     */
    private $cart;

    /**
     * Constructor.
     *
     * @param CGWFW_Settings $settings Settings helper.
     * @param CGWFW_Cart     $cart     Cart handler.
     */
    public function __construct( $settings, $cart ) {
        $this->settings = $settings;
        $this->cart = $cart;
    }

    /**
     * Integration name. Drives the script-data setting key
     * ("cart-gift-wrapping_data") read on the front end.
     *
     * @return string
     */
    public function get_name() {
        return 'cart-gift-wrapping';
    }

    /**
     * Register the scripts and styles used by the blocks.
     *
     * @return void
     */
    public function initialize() {
        wp_register_script(
            'cgwfw-blocks',
            CGWFW_URL . 'assets/js/cgwfw-blocks.js',
            array(
                'wp-element',
                'wp-i18n',
                'wp-data',
                'wp-plugins',
                'wc-blocks-checkout',
                'wc-settings'
            ),
            CGWFW_VERSION,
            true
        );
        // Let the JavaScript __() strings pick up translations (loaded from the
        // languages/ directory as JED .json files generated from the .po files).
        if ( function_exists( 'wp_set_script_translations' ) ) {
            wp_set_script_translations( 'cgwfw-blocks', 'tinyplugs-gift-wrapping', CGWFW_PATH . 'languages' );
        }
        wp_register_style(
            'cgwfw-blocks',
            CGWFW_URL . 'assets/css/cgwfw.css',
            array(),
            CGWFW_VERSION
        );
        // Inject the admin-configured button styling as CSS variables.
        wp_add_inline_style( 'cgwfw-blocks', $this->settings->get_inline_css() );
    }

    /**
     * Front-end script handles.
     *
     * @return string[]
     */
    public function get_script_handles() {
        return array('cgwfw-blocks');
    }

    /**
     * Editor script handles (same script powers the editor preview).
     *
     * @return string[]
     */
    public function get_editor_script_handles() {
        return array('cgwfw-blocks');
    }

    /**
     * Style handles.
     *
     * @return string[]
     */
    public function get_style_handles() {
        return array('cgwfw-blocks');
    }

    /**
     * Static configuration passed to the front end. Live state (enabled,
     * message, availability, fee) is delivered separately via the Store API.
     *
     * @return array
     */
    public function get_script_data() {
        $designs = array();
        return array(
            'mode'           => $this->settings->get( 'mode' ),
            'showOn'         => $this->settings->get( 'order_show_on' ),
            'hasDesigns'     => !empty( $designs ) && 'flat' !== $this->settings->designs_display(),
            'designsDisplay' => $this->settings->designs_display(),
            'designs'        => $designs,
            'designLabel'    => $this->settings->get( 'designs_label' ),
            'noneLabel'      => $this->settings->get( 'designs_none_label' ),
            'style'          => $this->settings->get( 'order_style' ),
            'orderLabel'     => $this->settings->get( 'order_label' ),
            'subtitle'       => $this->settings->get( 'order_subtitle' ),
            'addLabel'       => $this->settings->get( 'card_add_label' ),
            'addedLabel'     => $this->settings->get( 'card_added_label' ),
            'messageLabel'   => $this->settings->get( 'message_label' ),
            'enableMessage'  => 'yes' === $this->settings->get( 'enable_message' ),
            'messageLimit'   => (int) $this->settings->get( 'message_limit' ),
            'enableTooltip'  => 'yes' === $this->settings->get( 'enable_tooltip' ),
            'tooltipText'    => trim( (string) $this->settings->get( 'tooltip_text' ) ),
        );
    }

}
