<?php
if ( ! class_exists( 'WFOCU_Divi_Field' ) ) {
	abstract class WFOCU_Divi_Field extends ET_Builder_Module {
		protected $get_local_slug = '';
		protected $ajax = false;
		public $vb_support = 'on';
		protected $post_id = 0;
		protected $tabs = [];
		protected $sub_tabs = [];
		protected $html_fields = [];
		protected $section_fields = [];
		protected $modules_fields = [];
		protected $typography = [];
		protected $tab_array = [];
		protected $style_selector = [];
		protected static $product_options = [];
		protected $module_credits = array(
			'module_uri' => '',
			'author'     => 'FunnelKit',
			'author_uri' => 'https://funnelkit.com/',
		);

		public function __construct() {
			$name       = get_class( $this );
			$this->slug = 'et_' . strtolower( $name );
			parent::__construct();
			if ( true === $this->ajax ) {
				add_action( 'wp_ajax_' . $this->slug, [ $this, 'render_ajax' ] );
			}
			$this->advanced_fields['margin_padding']['use_margin']  = false;
			$this->advanced_fields['margin_padding']['use_padding'] = false;

		}

		/**
		 * Widget Name
		 *
		 * @param $name
		 */
		public function set_name( $name ) {
			$this->name = $name;
		}

		public function set_slug( $slug ) {
			$this->slug = 'wfocu_' . $slug;
		}

		protected function color_type() {
			return WFOCU_Divi_Extension::$field_color_type;
		}


		public function add_heading( $tab_id, $heading, $separator = '', $conditions = [] ) {
			$key                          = $this->get_unique_id();
			$this->modules_fields[ $key ] = array(
				'label'     => $heading,
				'type'      => 'text',
				'className' => 'wfocu_heading_divi_builder',
				'default'   => ''
			);


			if ( is_array( $conditions ) && ! empty( $conditions ) ) {


				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );

			return $key;
		}

		public function add_subheading( $tab_id, $heading, $separator = '', $conditions = [] ) {
			$key                          = $this->get_unique_id();
			$this->modules_fields[ $key ] = array(
				'label'     => $heading,
				'type'      => 'text',
				'className' => 'wfocu_subheading_divi_builder',
				'default'   => ''
			);


			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );

			return $key;
		}

		protected function add_switcher( $tab_id, $key, $label = '', $default = 'off', $conditions = [], $description = '' ) {
			if ( empty( $label ) ) {
				$label = __( 'Enable', 'woofunnels-upstroke-one-click-upsell' );
			}
			$this->modules_fields[ $key ] = array(
				'label'           => $label,
				'type'            => 'yes_no_button',
				'default'         => $default,
				'option_category' => 'configuration',
				'options'         => array(
					'on'  => esc_html__( 'Yes', 'et_builder' ),
					'off' => esc_html__( 'No', 'et_builder' ),
				),
			);
			if ( ! empty( $description ) ) {
				$this->modules_fields[ $key ]['description'] = $description;
			}

			$this->assign_tab( $key, $tab_id );
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}

			return $key;

		}

		protected function add_select( $tab_id, $key, $label, $options, $field_default_cls, $conditions = [] ) {
			$this->modules_fields[ $key ] = array(
				'label'   => $label,
				'type'    => 'select',
				'options' => $options,
				'default' => $field_default_cls
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );

			return $key;
		}

		public function add_text( $tab_id, $key, $label, $default = '', $conditions = [], $description = '', $placeholder = '' ) {

			$this->modules_fields[ $key ] = array(
				'label'     => $label,
				'type'      => 'textarea',
				'default'   => $default,
				'className' => 'wfocu_divi_textarea',
			);


			if ( ! empty( $description ) ) {
				$this->modules_fields[ $key ]['description'] = $description;
			}

			if ( ! empty( $placeholder ) ) {
				$this->modules_fields[ $key ]['placeholder'] = $placeholder;
			}
			if ( ! empty( $classes ) ) {
				$this->modules_fields[ $key ]['classes'] = $classes;
			}
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );


			return $key;
		}

		public function add_class( $key, $class ) {
			if ( isset( $this->modules_fields[ $key ] ) ) {
				$this->modules_fields[ $key ]['className'] = $class;
			}
		}

		protected function add_textArea( $tab_id, $key, $label, $default = '', $conditions = [] ) {

			$this->modules_fields[ $key ] = [
				'label'   => $label,
				'type'    => 'textarea',
				'default' => $default,
			];

			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );

			return $key;
		}

		protected function add_typography( $tab_id, $key, $selectors = '', $label = '', $default = '', $conditions = [], $font_side_default = [] ) {

			$keys = [];


			$keys[] = $this->add_font_size( $tab_id, $key, $selectors, '', '', $conditions, $font_side_default );
			$keys[] = $this->add_font( $tab_id, $key, $selectors, '', $default, $conditions );
			$keys[] = $this->add_line_height( $tab_id, $key, $selectors, '', '1px', $conditions );


			//$this->set_selector( $tab_id, $key, $selectors );

			return $keys;
		}


		protected function add_text_alignments( $tab_id, $key, $selectors = '', $label = '', $default = 'left', $conditions = [] ) {
			if ( empty( $label ) ) {
				$label = esc_html__( 'Alignment', 'et_builder' );
			}
			$this->modules_fields[ $key ] = array(
				'label'          => $label,
				'type'           => 'text_align',
				'mobile_options' => true,
				'default'        => $default,
				'options'        => et_builder_get_text_orientation_options( [ 'justified' ] ),
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		protected function add_font_size( $tab_id, $key, $selectors = '', $label = '', $default = [], $conditions = [], $font_side_default = [] ) {

			if ( empty( $label ) ) {
				$label = esc_html__( 'Font Size', 'et_builder' );
			}
			if ( empty( $default ) ) {
				$default = [ 'default' => '16px', 'unit' => 'px' ];
			}
			if ( ! empty( $font_side_default ) ) {
				$default = $font_side_default;
			}


			if ( ! isset( $default['range_settings'] ) ) {
				$default['range_settings'] = array(
					'min'  => '1',
					'max'  => '120',
					'step' => '1',
				);
			}
			if ( ! isset( $default['allowed_units'] ) ) {
				$default['allowed_units'] = array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' );
			}
			$this->typography[ $key . '_font_size' ] = $key . "_typograhy";
			$key                                     = $key . '_font_size';
			$this->modules_fields[ $key ]            = array(
				'label'           => $label,
				'type'            => 'range',
				'option_category' => 'font_option',
				'c_type'          => 'font_size',
				'default'         => $default['default'],
				'default_unit'    => $default['unit'],
				'allowed_units'   => $default['allowed_units'],
				'range_settings'  => $default['range_settings'],
				'responsive'      => true,
				'mobile_options'  => true,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}


		protected function add_letter_spacing( $tab_id, $key, $selectors = '', $label = '', $default = [], $conditions = [] ) {

			if ( empty( $label ) ) {
				$label = esc_html__( 'Letter Spacing', 'et_builder' );
			}
			if ( empty( $default ) ) {
				$default = [ 'default' => '0.9px', 'unit' => 'px' ];
			}
			if ( ! isset( $default['range_settings'] ) ) {
				$default['range_settings'] = array(
					'min'  => '1',
					'max'  => '10',
					'step' => '.2',
				);
			}
			if ( ! isset( $default['allowed_units'] ) ) {
				$default['allowed_units'] = array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' );
			}

			$this->modules_fields[ $key ] = [
				'label'           => $label,
				'type'            => 'range',
				'option_category' => 'font_option',
				'c_type'          => 'letter_spacing',
				'default'         => $default['default'],
				'default_unit'    => $default['unit'],

				'allowed_units'  => $default['allowed_units'],
				'range_settings' => $default['range_settings'],
				'responsive'     => true,
				'mobile_options' => true,
			];
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		protected function add_font( $tab_id, $key, $selectors = '', $label = '', $default = '#000000', $conditions = [] ) {
			if ( empty( $label ) ) {
				$label = esc_html__( 'Select Font', 'et_builder' );
			}
			$key = $key . '_typograhy';

			$this->modules_fields[ $key ] = array(
				'label'   => $label,
				'type'    => 'font',
				'default' => $default
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		protected function add_color( $tab_id, $key, $selectors = '', $label = 'Color', $default = '#000000', $conditions = [] ) {
			if ( empty( $label ) ) {
				$label = 'Color';
			}
			$this->modules_fields[ $key ] = array(
				'label'        => $label,
				'type'         => $this->color_type(),
				'custom_color' => true,
				'default'      => $default,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}

			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		public function add_background_color( $tab_id, $key, $selectors = [], $default = '#000000', $label = '', $conditions = [] ) {

			if ( empty( $label ) ) {
				$label = esc_attr__( 'Background', 'elementor' );
			}


			$this->modules_fields[ $key ] = array(
				'label'        => $label,
				'type'         => $this->color_type(),
				'c_type'       => 'background_color',
				'custom_color' => true,
				'default'      => $default,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}

			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		public function add_border_color( $tab_id, $key, $selectors = [], $default = '#000000', $label = '', $box_shadow = false, $conditions = [] ) {

			if ( empty( $label ) ) {
				$label = esc_attr__( 'Color', 'elementor' );
			}


			$this->modules_fields[ $key ] = array(
				'label'        => $label,
				'type'         => $this->color_type(),
				'c_type'       => 'border_color',
				'custom_color' => true,
				'default'      => $default,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;

		}

		protected function add_border_radius( $tab_id, $key, $selector, $conditions = [], $default = [], $custom_label = '' ) {

			$label = __( 'Border Radius', 'elementor' );

			if ( ! empty( $custom_label ) ) {
				$label = $custom_label;
			}


			if ( empty( $default ) ) {
				$default = [ 'default' => '0', 'unit' => 'px' ];
			}

			$this->modules_fields[ $key ] = array(
				'label'            => $label,
				'type'             => 'range',
				'option_category'  => 'button',
				'c_type'           => 'border_radius',
				'default'          => $default['default'],
				'default_unit'     => $default['unit'],
				'default_on_front' => '',
				'allowed_units'    => [ 'px', 'em', '%' ],
				'hover'            => 'tabs',
				'mobile_options'   => true,
			);


			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selector );

			return $key;

		}

		protected function add_padding( $tab_id, $key, $selector, $default = '', $label = '', $conditions = [] ) {

			if ( empty( $label ) ) {
				$label = esc_html__( 'Padding', 'et_builder' );
			}


			$this->modules_fields[ $key ] = array(
				'label'           => $label,
				'type'            => 'custom_padding',
				'c_type'          => 'padding',
				'mobile_options'  => true,
				'default'         => $default,
				'option_category' => 'layout',
				'hover'           => 'tabs',
				'allowed_units'   => array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' ),
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selector );

			return $key;
		}

		protected function add_margin( $tab_id, $key, $selector, $default = '', $label = '', $conditions = [] ) {

			if ( empty( $label ) ) {
				$label = esc_html__( 'Margin', 'et_builder' );
			}
			$this->modules_fields[ $key ] = array(
				'label'           => $label,
				'type'            => 'custom_margin',
				'c_type'          => 'margin',
				'mobile_options'  => true,
				'default'         => $default,
				'option_category' => 'layout',
				'hover'           => 'tabs',
				'allowed_units'   => array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' ),
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selector );

			return $key;
		}

		protected function add_border( $tab_id, $key, $selectors, $conditions = [], $default = [], $fields_options = [] ) {


			$border_option = [
				'none'   => __( 'None', 'woofunnels-upstroke-one-click-upsell' ),
				'solid'  => __( 'Solid', 'woofunnels-upstroke-one-click-upsell' ),
				'double' => __( 'Double', 'woofunnels-upstroke-one-click-upsell' ),
				'dotted' => __( 'Dotted', 'woofunnels-upstroke-one-click-upsell' ),
				'dashed' => __( 'Dashed', 'woofunnels-upstroke-one-click-upsell' ),
				'groove' => __( 'Groove', 'woofunnels-upstroke-one-click-upsell' ),
			];

			$default_args = [
				'border_type'          => 'solid',
				'border_width_top'     => '1',
				'border_width_bottom'  => '1',
				'border_width_left'    => '1',
				'border_width_right'   => '1',
				'border_radius_top'    => '0',
				'border_radius_bottom' => '0',
				'border_radius_left'   => '0',
				'border_radius_right'  => '0',
				'border_color'         => '#dddddd',
			];

			$fields_keys = [];
			$default     = wp_parse_args( $default, $default_args );

			$wfocu_start_border                          = $this->get_unique_id();
			$this->modules_fields[ $wfocu_start_border ] = array(
				'label'     => 'start border',
				'type'      => 'hidden',
				'c_type'    => 'wfocu_start_border',
				'field_key' => $key,
				'selector'  => $selectors
			);

			$fields_keys[]  = $this->add_subheading( $tab_id, __( 'Border', 'woofunnels-upstroke-one-click-upsell', '', $conditions ) );
			$border_type    = $this->add_select( $tab_id, $key . '_border_type', __( 'Type', 'woofunnels-upstroke-one-click-upsell' ), $border_option, $default['border_type'] );
			$fields_keys[]  = $border_type;
			$type_condition = [ $border_type => [ 'solid', 'double', 'dotted', 'dashed', 'groove' ] ];
			if ( ! empty( $conditions ) ) {
				$type_condition = array_merge( $type_condition, $conditions );
			}


			$fields_keys[] = $this->add_subheading( $tab_id, __( 'Width', 'woofunnels-upstroke-one-click-upsell' ), '', $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_width_top', __( 'Top', 'woofunnels-upstroke-one-click-upsell' ), $default['border_width_top'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_width_bottom', __( 'Bottom', 'woofunnels-upstroke-one-click-upsell' ), $default['border_width_bottom'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_width_left', __( 'Left', 'woofunnels-upstroke-one-click-upsell' ), $default['border_width_left'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_width_right', __( 'Right', 'woofunnels-upstroke-one-click-upsell' ), $default['border_width_right'], $type_condition );
			$fields_keys[] = $this->add_color( $tab_id, $key . '_border_color', $selectors, '', $default['border_color'], $type_condition );

			$fields_keys[] = $this->add_subheading( $tab_id, __( 'Border Radius', 'woofunnels-upstroke-one-click-upsell' ), '', $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_radius_top', __( 'Top', 'woofunnels-upstroke-one-click-upsell' ), $default['border_radius_top'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_radius_bottom', __( 'Bottom', 'woofunnels-upstroke-one-click-upsell' ), $default['border_radius_bottom'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_radius_left', __( 'Left', 'woofunnels-upstroke-one-click-upsell' ), $default['border_radius_left'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_border_radius_right', __( 'Right', 'woofunnels-upstroke-one-click-upsell' ), $default['border_radius_right'], $type_condition );

			$wfocu_end_border                          = $this->get_unique_id();
			$this->modules_fields[ $wfocu_end_border ] = array(
				'label'     => 'end border',
				'type'      => 'hidden',
				'c_type'    => 'wfocu_end_border',
				'field_key' => $key
			);

			$this->add_class( $key . '_border_width_top', 'wfocu_divi_border_width_start wfocu_border_width_top' );
			$this->add_class( $key . '_border_width_bottom', 'wfocu_border_width_bottom' );
			$this->add_class( $key . '_border_width_left', 'wfocu_border_width_left' );
			$this->add_class( $key . '_border_width_right', 'wfocu_divi_border_width_start wfocu_border_width_right' );

			$this->add_class( $key . '_border_radius_top', 'wfocu_divi_border_width_start wfocu_border_width_top' );
			$this->add_class( $key . '_border_radius_bottom', 'wfocu_border_width_bottom' );
			$this->add_class( $key . '_border_radius_left', 'wfocu_border_width_left' );
			$this->add_class( $key . '_border_radius_right', 'wfocu_divi_border_width_end wfocu_border_width_right' );


			return $fields_keys;
		}

		protected function add_box_shadow( $tab_id, $key, $selectors, $default = [], $conditions = [] ) {


			$border_option = [
				''      => __( 'Outline', 'woofunnels-upstroke-one-click-upsell' ),
				'inset' => __( 'Inset', 'woofunnels-upstroke-one-click-upsell' ),
			];


			$default_args = [
				'enable'     => 'off',
				'type'       => '',
				'horizontal' => '0',
				'vertical'   => '0',
				'blur'       => '0',
				'spread'     => '0',
				'color'      => '#dddddd',
			];


			$fields_keys                                 = [];
			$default                                     = wp_parse_args( $default, $default_args );
			$wfocu_start_border                          = $this->get_unique_id();
			$this->modules_fields[ $wfocu_start_border ] = array(
				'label'     => 'start box shadow',
				'type'      => 'hidden',
				'c_type'    => 'wfocu_start_box_shadow',
				'field_key' => $key,
				'selector'  => $selectors
			);


			$fields_keys[] = $this->add_subheading( $tab_id, __( 'Box Shadow', 'woofunnels-upstroke-one-click-upsell', '', $conditions ) );
			$enabled       = $this->add_switcher( $tab_id, $key . '_shadow_enable', __( 'Enable', 'woofunnels-upstroke-one-click-upsell' ), $default['enable'], $conditions );

			$type_condition = [ $enabled => 'on' ];
			if ( ! empty( $conditions ) ) {
				$type_condition = array_merge( $type_condition, $conditions );
			}
			$fields_keys[] = $this->add_select( $tab_id, $key . '_shadow_type', __( 'Position', 'woofunnels-upstroke-one-click-upsell' ), $border_option, $default['type'], $type_condition );
			$fields_keys[] = $this->add_color( $tab_id, $key . '_shadow_color', $selectors, '', $default['color'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_shadow_horizontal', __( 'Horizontal', 'woofunnels-upstroke-one-click-upsell' ), $default['horizontal'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_shadow_vertical', __( 'Vertical', 'woofunnels-upstroke-one-click-upsell' ), $default['vertical'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_shadow_blur', __( 'Blur', 'woofunnels-upstroke-one-click-upsell' ), $default['blur'], $type_condition );
			$fields_keys[] = $this->add_text( $tab_id, $key . '_shadow_spread', __( 'Spread', 'woofunnels-upstroke-one-click-upsell' ), $default['spread'], $type_condition );


			$wfocu_end_border                          = $this->get_unique_id();
			$this->modules_fields[ $wfocu_end_border ] = array(
				'label'     => 'end border',
				'type'      => 'hidden',
				'c_type'    => 'wfocu_end_box_shadow',
				'field_key' => $key
			);

			$this->add_class( $key . '_shadow_horizontal', 'wfocu_divi_border_width_start wfocu_border_width_top' );
			$this->add_class( $key . '_shadow_vertical', 'wfocu_border_width_bottom' );
			$this->add_class( $key . '_shadow_blur', 'wfocu_border_width_left' );
			$this->add_class( $key . '_shadow_spread', 'wfocu_divi_border_width_start wfocu_border_width_right' );

			return $fields_keys;
		}

		protected function add_line_height( $tab_id, $key, $selectors = '', $label = '', $default = '1px', $conditions = [] ) {
			$key                          .= '_line_height';
			$this->modules_fields[ $key ] = array(
				'label'           => esc_html__( 'Line Height', 'et_builder' ),
				'type'            => 'range',
				'c_type'          => 'line_height',
				'mobile_options'  => true,
				'option_category' => 'font_option',
				'default_unit'    => 'em',
				'default'         => "1.5",
				'allowed_units'   => array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' ),
				'range_settings'  => array(
					'min'  => '1',
					'max'  => '45',
					'step' => '0.1',
				),
				'hover'           => 'tabs',
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;

		}

		protected function add_divider( $type ) {

		}

		protected function add_width( $tab_id, $key, $selectors = '', $label = '', $default = [], $conditions = [], $responsive = false ) {

			if ( empty( $label ) ) {
				$label = esc_html__( 'Width', 'et_builder' );
			}
			if ( empty( $default ) ) {
				$default = [ 'default' => '100', 'unit' => '%' ];
			}
			if ( ! isset( $default['range_settings'] ) ) {
				$default['range_settings'] = array(
					'min'  => '1',
					'max'  => '100',
					'step' => '1',
				);
			}
			if ( ! isset( $default['allowed_units'] ) ) {
				$default['allowed_units'] = array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' );
			}


			$key                          = $key . '_width';
			$this->modules_fields[ $key ] = array(
				'label'           => $label,
				'type'            => 'range',
				'option_category' => 'configuration',
				'c_type'          => 'width',
				'default'         => $default['default'],
				'default_unit'    => $default['unit'],
				'allowed_units'   => $default['allowed_units'],
				'range_settings'  => $default['range_settings'],
				'responsive'      => $responsive,
				'mobile_options'  => $responsive,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		protected function add_min_width( $tab_id, $key, $selectors = '', $label = '', $default = [], $conditions = [] ) {

			if ( empty( $label ) ) {
				$label = esc_html__( 'Min Width', 'et_builder' );
			}
			if ( empty( $default ) ) {
				$default = [ 'default' => '100', 'unit' => '%' ];
			}
			if ( ! isset( $default['range_settings'] ) ) {
				$default['range_settings'] = array(
					'min'  => '1',
					'max'  => '100',
					'step' => '1',
				);
			}
			if ( ! isset( $default['allowed_units'] ) ) {
				$default['allowed_units'] = array( '%', 'em', 'rem', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw' );
			}
			$key                          = $key . '_width';
			$this->modules_fields[ $key ] = array(
				'label'            => $label,
				'type'             => 'range',
				'option_category'  => 'configuration',
				'c_type'           => 'min_width',
				'default'          => $default['default'],
				'default_unit'     => $default['unit'],
				'default_on_front' => '',
				'allowed_units'    => $default['allowed_units'],
				'range_settings'   => $default['range_settings'],
				'responsive'       => false,
				'mobile_options'   => false,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		protected function add_controls_tabs( $tab_id, $label, $conditions = [] ) {

			$key                          = $this->get_unique_id();
			$this->modules_fields[ $key ] = array(
				'label'               => $label,
				'attr_suffix'         => '',
				'type'                => 'composite',
				'composite_type'      => 'default',
				'composite_structure' => array(),
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );

			return $key;
		}

		protected function add_controls_tab( $control_tabs_id, $label, $field_keys, $id = '' ) {
			$id = $this->get_unique_id();
			if ( isset( $this->modules_fields[ $control_tabs_id ] ) ) {
				$this->modules_fields[ $control_tabs_id ]['composite_structure'][ $id ]['label'] = $label;
				$controls                                                                        = [];

				foreach ( $field_keys as $field_key ) {
					if ( ! isset( $this->modules_fields[ $field_key ] ) ) {

						continue;
					}


					$controls[ $field_key ] = $this->modules_fields[ $field_key ];


					if ( count( $controls ) > 0 ) {
						$this->tab_array[ $field_key ] = $this->modules_fields[ $field_key ];
					}

					unset( $controls[ $field_key ]['toggle_slug'] );
					unset( $controls[ $field_key ]['tab_slug'] );
					unset( $this->modules_fields[ $field_key ] );

				}
				if ( count( $controls ) > 0 ) {
					$this->modules_fields[ $control_tabs_id ]['composite_structure'][ $id ]['controls'] = $controls;

				}
			}


			return [ $id, $control_tabs_id ];
		}

		public function add_animation_fields( $tab_id, $key, $selectors = '', $label = 'Animation Style', $default = 'none', $conditions = [] ) {


			$this->modules_fields[ $key ] = array(
				'label'           => $label,
				'type'            => 'select_animation',
				'option_category' => 'configuration',
				'default'         => $default,
				'description'     => esc_html__( 'Pick an animation style to enable animations for this element. Once enabled, you will be able to customize your animation style further. To disable animations, choose the None option.', 'et_builder' ),
				'options'         => array(
					'none'   => esc_html__( 'None', 'et_builder' ),
					'fade'   => esc_html__( 'Fade', 'et_builder' ),
					'slide'  => esc_html__( 'Slide', 'et_builder' ),
					'bounce' => esc_html__( 'Bounce', 'et_builder' ),
					'zoom'   => esc_html__( 'Zoom', 'et_builder' ),
					'flip'   => esc_html__( 'Flip', 'et_builder' ),
					'fold'   => esc_html__( 'Fold', 'et_builder' ),
					'roll'   => esc_html__( 'Roll', 'et_builder' ),
				),
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );
			$this->set_selector( $tab_id, $key, $selectors );

			return $key;
		}

		protected function add_icon( $tab_id, $key, $label = '', $default = '', $conditions = [] ) {
			if ( empty( $label ) ) {
				$label = esc_html__( 'Icon', 'et_builder' );
			}
			$this->modules_fields[ $key ] = array(
				'label'           => $label,
				'type'            => 'select_icon',
				'option_category' => 'configuration',
				'class'           => array( 'et-pb-font-icon' ),
				'mobile_options'  => false,
			);
			if ( is_array( $conditions ) && ! empty( $conditions ) ) {
				$this->show_if( $key, $conditions );
			}
			$this->assign_tab( $key, $tab_id );

		}

		protected function set_selector( $tab_id, $key, $selector, $value = '' ) {
			if ( empty( $selector ) ) {
				return;
			}
			$selector = is_array( $selector ) ? implode( ',', $selector ) : $selector;

			$selector = str_replace( '%%order_class%%', '%%order_class%% #' . $this->slug, $selector );

			$type                                     = isset( $this->modules_fields[ $key ]['c_type'] ) ? $this->modules_fields[ $key ]['c_type'] : ( isset( $this->modules_fields[ $key ]['type'] ) ? $this->modules_fields[ $key ]['type'] : '' );
			$this->modules_fields[ $key ]['selector'] = $selector;
			$css_data                                 = $this->create_css_property( $key, $type );
			if ( empty( $css_data ) ) {
				return;
			}

			$property       = $css_data['property'];
			$property_value = $css_data['value'];
			if ( ! empty( $property_value ) ) {
				$this->style_selector[ $selector ][ $property ] = $property_value;
			}
		}

		protected function create_css_property( $key, $type ) {
			$property = [];
			$default  = isset( $this->modules_fields[ $key ]['default'] ) ? $this->modules_fields[ $key ]['default'] : '';


			switch ( $type ) {
				case  'text_align':
					$default  = isset( $this->modules_fields[ $key ]['default'] ) ? $this->modules_fields[ $key ]['default'] : ( is_rtl() ? 'right' : 'left' );
					$property = [ 'property' => 'text-align', 'value' => $default ];
					break;
				case  'letter_spacing':
					$property = [ 'property' => 'letter-spacing', 'value' => $default ];
					break;
				case  'line_height':
					$property = [ 'property' => 'line-height', 'value' => $default ];
					break;
				case  'width':
					$property = [ 'property' => 'width', 'value' => $default ];
					break;
				case  'min_width':
					$property = [ 'property' => 'min-width', 'value' => $default ];
					break;
				case  'margin':
					$property = [ 'property' => 'margin', 'value' => $default ];
					break;
				case  'padding':
					$property = [ 'property' => 'margin', 'value' => $default ];
					break;
				case  'border_radius':
					$property = [ 'property' => 'border-radius', 'value' => $default ];
					break;
				case  'border_color':
					$property = [ 'property' => 'border-color', 'value' => $default ];
					break;
				case  'background_color':
					$property = [ 'property' => 'background-color', 'value' => $default ];
					break;
				case  'color':
					$property = [ 'property' => 'color', 'value' => $default ];
					break;
				case  'color-alpha':
					$property = [ 'property' => 'color', 'value' => $default ];
					break;
				case  'font_size':
					$property = [ 'property' => 'font-size', 'value' => $default ];
					break;
				case  'box_shadow':
					$property = [ 'property' => 'box-shadow', 'value' => '' ];
					break;
				default:
					break;

			}


			return $property;
		}


		protected function get_class_options() {
			return [
				'wfocu-col-full'       => __( 'Full', 'woofunnels-upstroke-one-click-upsell' ),
				'wfocu-col-left-half'  => __( 'One Half', 'woofunnels-upstroke-one-click-upsell' ),
				'wfocu-col-left-third' => __( 'One Third', 'woofunnels-upstroke-one-click-upsell' ),
				'wfocu-col-two-third'  => __( 'Two Third', 'woofunnels-upstroke-one-click-upsell' ),
			];
		}

		protected function show_if( $key, $condition ) {
			$this->modules_fields[ $key ]['show_if'] = $condition;
		}

		protected function show_if_not( $key, $condition ) {
			$this->modules_fields[ $key ]['show_if_not'] = $condition;


		}

		protected function add_responsive_control( $key ) {
			if ( isset( $this->modules_fields[ $key ] ) ) {
				$this->modules_fields[ $key ]['mobile_options'] = true;
			}
		}


		public function get_complete_fields() {

			add_filter( 'et_builder_module_general_fields', '__return_empty_array' );
			$fields = parent::get_complete_fields();
			remove_filter( 'et_builder_module_general_fields', '__return_empty_array' );

			return $fields;
		}

		protected function assign_tab( $key, $tab_id ) {


			if ( isset( $this->modules_fields[ $key ] ) ) {
				$this->modules_fields[ $key ]['toggle_slug'] = $tab_id;
				$this->modules_fields[ $key ]['tab_slug']    = $this->tabs[ $tab_id ]['type'];
			}
		}

		protected function assign_sub_tab( $key, $sub_tab_id ) {
			if ( isset( $this->modules_fields[ $key ] ) ) {
				$this->modules_fields[ $key ]['sub_toggle'] = $sub_tab_id;
			}
		}

		protected function add_tab( $name, $type, $id = '' ) {

			if ( $type == '5' ) {
				$type = 'general';
			} else if ( $type == '3' ) {
				$type = 'custom_css';
			} else if ( $type == '2' ) {
				$type = 'advanced';
			} else {
				$type = 'general';
			}
			//advanced

			if ( empty( $id ) ) {
				$id = $this->get_unique_id();
			}
			if ( isset( $this->settings_modal_toggles[ $type ] ) ) {
				$this->settings_modal_toggles[ $type ]['toggles'][ $id ] = [ 'title' => $name ];
			} else {
				$this->settings_modal_toggles[ $type ] = [ 'toggles' => [ $id => [ 'title' => $name ] ] ];
			}
			$this->tabs[ $id ] = [ 'type' => $type, 'name' => $name ];

			return $id;
		}

		protected function add_sub_tab( $name, $tab_id, $id = '' ) {
			if ( empty( $id ) ) {
				$id = $this->get_unique_id();
			}
			$tab  = $this->tabs[ $tab_id ];
			$type = $tab['type'];

			if ( isset( $this->settings_modal_toggles[ $type ]['toggles'][ $tab_id ] ) ) {
				$this->settings_modal_toggles[ $type ]['toggles'][ $tab_id ]['tabbed_subtoggles']   = true;
				$this->settings_modal_toggles[ $type ]['toggles'][ $tab_id ]['sub_toggles'] [ $id ] = [ 'name' => $name ];
			} else {
				$this->settings_modal_toggles[ $type ]['toggles'][ $tab_id ]['tabbed_subtoggles']   = true;
				$this->settings_modal_toggles[ $type ]['toggles'][ $tab_id ]['sub_toggles'] [ $id ] = [ 'name' => $name ];
			}

			$this->sub_tabs[ $id ] = [ 'type' => $type, 'id' => $id, 'name' => $name, 'tab_id' => $tab_id ];

			return $this->sub_tabs[ $id ];
		}

		protected function _add_link_options_fields() {
			$this->_additional_fields_options = [];
		}

		public function get_fields() {


			return $this->modules_fields;
		}

		protected function get_unique_id() {
			static $count = 0;
			$count ++;
			$key = md5( 'wfocu_' . $count );

			return $key;
		}

		protected function get_name() {
			return $this->name;
		}

		protected function get_slug() {
			return $this->slug;
		}

		protected function get_local_slug() {
			return $this->get_local_slug;
		}


		public function init() {

			global $post;
			$post_type = WFOCU_Common::get_offer_post_type_slug();

			if ( wp_doing_ajax() ) {
				if ( isset( $_REQUEST['action'] ) && "et_fb_get_saved_templates" == $_REQUEST['action'] && isset( $_REQUEST['et_post_type'] ) && $post_type !== $_REQUEST['et_post_type'] ) {
					return;
				}
				if ( isset( $_REQUEST['action'] ) && "et_fb_update_builder_assets" == $_REQUEST['action'] && isset( $_REQUEST['et_post_type'] ) && $post_type !== $_REQUEST['et_post_type'] ) {
					return;
				}
				$post_id = 0;
				if ( isset( $_REQUEST['action'] ) && "heartbeat" == $_REQUEST['action'] && isset( $_REQUEST['data'] ) ) {
					if ( isset( $_REQUEST['data']['et'] ) ) {
						$post_id = $_REQUEST['data']['et']['post_id'];

					}
				}
				if ( isset( $_REQUEST['post_id'] ) ) {
					$post_id = absint( $_REQUEST['post_id'] );
				}
				if ( isset( $_REQUEST['et_post_id'] ) ) {
					$post_id = absint( $_REQUEST['et_post_id'] );
				}
				if ( $post_id > 0 ) {
					$post = get_post( $post_id );
					if ( is_null( $post ) || $post->post_type !== $post_type ) {
						return;
					}
					WFOCU_Core()->public->is_preview = true;
					add_filter( 'wfocu_valid_state_for_data_setup', '__return_true' );
					WFOCU_Core()->template_loader->offer_id = $post_id;
					WFOCU_Core()->template_loader->maybe_setup_offer();
				}
			}

			if ( ! is_null( $post ) && $post->post_type !== $post_type ) {
				return;
			}

			$this->setup_offer();
			$this->setup_data();

		}

		public function render_ajax() {

			$this->props = $_REQUEST;

			echo "<div id='{$this->slug}'>" . $this->html( [] ) . '</div>';
			exit;
		}

		private function setup_offer() {
			if ( empty( self::$product_options ) ) {
				self::$product_options = array( '0' => __( '--No Product--', 'woofunnels-upstroke-one-click-upsell' ) );;

				if ( is_null( WFOCU_Core()->template_loader->product_data ) ) {
					return;
				}
				$products = WFOCU_Core()->template_loader->product_data->products;
				if ( ! empty( $products ) ) {
					self::$product_options = [];
					foreach ( $products as $key => $product ) {
						self::$product_options[ $key ] = $product->data->get_name();
					}
				}
			}
		}

		protected function setup_data() {
		}
	}
}