<?php
namespace Cool_FormKit_Pro\Widgets\HelloPlusAddons;

use HelloPlus\Modules\Forms\Widgets\Ehp_Form as Base_Form;

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


class Custom_Ehp_Form extends Base_Form {

    public function __construct( $data = [], $args = null ) {
        parent::__construct( $data, $args );

        // instantiate handlers so they hook into render_field actions
        static $custom_fields_loaded = false;

        if ( ! $custom_fields_loaded ) {
            $custom_fields_loaded = true;
        }
    }

    protected function add_content_form_fields_section(): void {
        parent::add_content_form_fields_section();
        
        $stack_id = $this->get_unique_name();
        $control  = \Elementor\Plugin::instance()
            ->controls_manager
            ->get_control_from_stack( $stack_id, 'form_fields' );

        if ( is_wp_error( $control ) || empty( $control['fields'] ) || ! is_array( $control['fields'] ) ) {
            return;
        }

        $fields = $control['fields'];

        if($this->is_field_enabled('country_code')){
            $fields['field_type']['options']['country']     = esc_html__( 'Country',     'cool-formkit' );

            $fields['field_type']['options']['state']     = esc_html__( 'State',     'cool-formkit' );
        }
        if($this->is_field_enabled('range_slider')){
            $fields['field_type']['options']['slider']     = esc_html__( 'Range Slider',     'cool-formkit' );
        }
        if($this->is_field_enabled('WYSIWYG')){
            $fields['field_type']['options']['WYSIWYG']     = esc_html__( 'WYSIWYG',     'cool-formkit' );
        }
        if($this->is_field_enabled('rating_field')){
            $fields['field_type']['options']['rating']     = esc_html__( 'Rating',     'cool-formkit' );
        }
        if($this->is_field_enabled('signature_field')){
            $fields['field_type']['options']['signature']     = esc_html__( 'Signature',     'cool-formkit' );
        }
        if($this->is_field_enabled('calculator_field')){
            $fields['field_type']['options']['calculator']     = esc_html__( 'Calculator',     'cool-formkit' );
        }
        if($this->is_field_enabled('image_radio')){
            $fields['field_type']['options']['image_radio']     = esc_html__( 'Image Radio',     'cool-formkit' );
        }
        if($this->is_field_enabled('currency_field')){
            $fields['field_type']['options']['currency']     = esc_html__( 'Currency',     'cool-formkit' );
        }
        if($this->is_field_enabled('cloudflare_recaptcha')){
            $fields['field_type']['options']['cloudflare_recaptcha']     = esc_html__( 'Cloudflare Turnstile',     'cool-formkit' );
        }
        if($this->is_field_enabled('h_recaptcha')){
            $fields['field_type']['options']['h_recaptcha']     = esc_html__( 'hCAPTCHA',     'cool-formkit' );
        }
        if($this->is_field_enabled('month_week_field')){
            $fields['field_type']['options']['monthWeek']     = esc_html__( 'Month/Week',     'cool-formkit' );
        }
        if($this->is_field_enabled('toggle_field')){
            $fields['field_type']['options']['toggle']     = esc_html__( 'Toggle',     'cool-formkit' );
        }

        
        $fields['field_type']['options']['radio']     = esc_html__( 'Radio',     'cool-formkit' );
        $fields['field_type']['options']['checkbox']     = esc_html__( 'Checkbox',     'cool-formkit' );
        $fields['field_type']['options']['date']     = esc_html__( 'Date',     'cool-formkit' );

        // ensure options textarea shows up for radio & checkbox
        $vals = &$fields['field_options']['conditions']['terms'][0]['value'];
        foreach ( [ 'radio', 'checkbox' ] as $type ) {
            if ( ! in_array( $type, $vals, true ) ) {
                $vals[] = $type;
            }
        }

        $this->update_control( 'form_fields', [ 'fields' => $fields ] );
    }

    public function get_name() {
        return 'ehp-form';
    }

    public function get_title() {
        return esc_html__( 'Form Lite', 'cool-formkit-for-elementor-forms' );
    }

    public function get_icon() {
        return 'eicon-ehp-forms';
    }

    private function is_field_enabled($field_key) {
        $enabled_elements = get_option('cfkef_enabled_elements', array());
        return in_array(sanitize_key($field_key), array_map('sanitize_key', $enabled_elements));
    }
}
