import { __ } from '@wordpress/i18n';
import { Button, TextControl, SelectControl } from '@wordpress/components';
import BoxShadowControl from './BoxShadowControl';
import TransitionControl from './TransitionControl';
import FilterControl from './FilterControl';
import TextShadowControl from './TextShadowControl';
import TransformControl from './TransformControl';
import { DEFAULT_TRANSFORM_VALUES } from './transformUtils';
import { getInheritedResponsiveValue, getResponsivePlaceholder, getResponsiveSelectOptions } from '../../controls/responsiveValueUtils';

export default function EffectsHover({ activeDevice, effect__hover, setAttributes }) {
  const deviceVal = effect__hover[activeDevice] || {};
  const inheritedEffect = getInheritedResponsiveValue(effect__hover, activeDevice) || {};

  const visibilityOptions = [
    { label: __('Default', 'axcelersblocks'), value: '' },
    { label: __('Visible', 'axcelersblocks'), value: 'visible' },
    { label: __('Hidden', 'axcelersblocks'), value: 'hidden' },
    { label: __('Collapse', 'axcelersblocks'), value: 'collapse' },
  ];
  const visibilitySelectOptions = getResponsiveSelectOptions({
    values: effect__hover,
    activeDevice,
    selector: (value) => value?.visibility || '',
    options: visibilityOptions,
    inheritedLabel: __('Inherited', 'axcelersblocks'),
  });

  const updateDeviceEffect = (key, value) => {
    setAttributes({
      effect__hover: {
        ...effect__hover,
        [activeDevice]: {
          ...deviceVal,
          [key]: value,
        },
      },
    });
  };

  const resetEffectHover = () => {
    setAttributes({
      effect__hover: {
        ...effect__hover,
        [activeDevice]: {
          boxShadow: { xOffset:'', yOffset:'', blurRadius:'', spreadRadius:'', color:'', inset:false },
          transition: { property:'', duration:'', timingFunction:'', delay:'' },
          filter: { blur:'', brightness:'', contrast:'', grayscale:'', hueRotate:'', invert:'', opacity:'', saturate:'', sepia:'' },
          textShadow: { xOffset:'', yOffset:'', blurRadius:'', color:'' },
          transform: { ...DEFAULT_TRANSFORM_VALUES },
          opacity: '',
          visibility: '',
        },
      },
    });
  };

  return (
    <>
      <BoxShadowControl activeDevice={activeDevice} effectValues={effect__hover} effect={deviceVal} inheritedEffect={inheritedEffect} updateDeviceEffect={updateDeviceEffect} />
      <TransitionControl effect={deviceVal} inheritedEffect={inheritedEffect} updateDeviceEffect={updateDeviceEffect} />
      <FilterControl effect={deviceVal} inheritedEffect={inheritedEffect} updateDeviceEffect={updateDeviceEffect} />
      <TextShadowControl activeDevice={activeDevice} effectValues={effect__hover} effect={deviceVal} inheritedEffect={inheritedEffect} updateDeviceEffect={updateDeviceEffect} />
      <TransformControl effect={deviceVal} inheritedEffect={inheritedEffect} updateDeviceEffect={updateDeviceEffect} />

      <TextControl
        className="effect-opacity-control"
        label={__('Opacity (Hover)', 'axcelersblocks')}
        value={deviceVal.opacity || ''}
        onChange={(v) => updateDeviceEffect('opacity', v)}
        placeholder={getResponsivePlaceholder({
          values: effect__hover,
          activeDevice,
          selector: (value) => value?.opacity || '',
          fallback: 'e.g., 0.5',
        })}
      />

      <SelectControl
        className="effect-visibility-control"
        label={__('Visibility (Hover)', 'axcelersblocks')}
        value={deviceVal.visibility || ''}
        options={visibilitySelectOptions}
        onChange={(v) => updateDeviceEffect('visibility', v)}
      />

      <Button className="reset-btn" onClick={resetEffectHover} showTooltip label={__('Reset', 'axcelersblocks')}>
        {__('Reset', 'axcelersblocks')}
      </Button>
    </>
  );
}
