import { __ } from "@wordpress/i18n";
import { RangeControl, SelectControl } from "@wordpress/components";
import ResponsiveControlV2 from "../../ResponsiveControlV2";
import { useResponsiveTabV2 } from "../../ResponsiveTabContextV2";
import PanelBodyWithSync from "../../PanelBodyWithSync";
import { getEnabledBreakpoints } from "../../../../../core/breakpoints";

const IconFrameRadiusSettingV6 = ({ 
  attributes, 
  setAttributes, 
  title, 
  initialOpen = false, 
  onSync,
  isHoverState = false, 
  supportsHover = true 
}) => {
  const effectiveHoverState = supportsHover ? isHoverState : false;
  const suffix = effectiveHoverState ? "Hover" : "";
  const responsiveAttrName = effectiveHoverState ? "responsiveIconFrameRadiusHover" : "responsiveIconFrameRadius";
  const responsiveEnabled = attributes[responsiveAttrName] !== undefined ? attributes[responsiveAttrName] : false;
  const { activeTab } = useResponsiveTabV2();
  const enabledBreakpoints = getEnabledBreakpoints();

  const handleResponsiveToggle = (value) => {
    setAttributes({ [responsiveAttrName]: value });

    const syncAttrs = {};
    enabledBreakpoints.slice(1).forEach(bp => {
      syncAttrs[`iconFrameRadius${bp.toUpperCase()}${suffix}`] = attributes[`iconFrameRadiusPC${suffix}`];
      syncAttrs[`iconFrameRadiusUnit${bp.toUpperCase()}${suffix}`] = attributes[`iconFrameRadiusUnitPC${suffix}`];
    });
    setAttributes(syncAttrs);
  };

  const renderRadiusControls = (tabName) => {
    const deviceSuffix = tabName.toUpperCase();
    const radiusValue = attributes[`iconFrameRadius${deviceSuffix}${suffix}`] ?? attributes.iconFrameRadius ?? 0;
    const unitValue = attributes[`iconFrameRadiusUnit${deviceSuffix}${suffix}`] ?? attributes.iconFrameRadiusUnit ?? "px";

    const handleRadiusChangeForDevice = (value) => {
      if (!responsiveEnabled) {
        const attrs = {};
        enabledBreakpoints.forEach(bp => {
          attrs[`iconFrameRadius${bp.toUpperCase()}${suffix}`] = value;
        });
        setAttributes(attrs);
      } else {
        setAttributes({ [`iconFrameRadius${deviceSuffix}${suffix}`]: value });
      }
    };

    const handleUnitChangeForDevice = (unit) => {
      if (!responsiveEnabled) {
        const attrs = {};
        enabledBreakpoints.forEach(bp => {
          attrs[`iconFrameRadiusUnit${bp.toUpperCase()}${suffix}`] = unit;
        });
        setAttributes(attrs);
      } else {
        setAttributes({ [`iconFrameRadiusUnit${deviceSuffix}${suffix}`]: unit });
      }
    };

    return (
      <div style={{ display: "flex", alignItems: "flex-end", gap: "8px" }}>
        <div style={{ flex: 1 }}>
          <RangeControl value={radiusValue} onChange={handleRadiusChangeForDevice} min={0} max={999} step={1} />
        </div>
        <div style={{ minWidth: "40px" }}>
          <SelectControl
            value={unitValue}
            options={[
              { label: "px", value: "px" },
              { label: "%", value: "%" },
            ]}
            onChange={handleUnitChangeForDevice}
          />
        </div>
      </div>
    );
  };

  return (
    <PanelBodyWithSync 
      title={title || __("Border Radius", "zenblocks")} 
      initialOpen={initialOpen} 
      onSync={onSync} 
      className="zenblocks-nested-panel"
    >
      <ResponsiveControlV2
        initialOpen={true}
        enableResponsiveToggle={true}
        responsiveEnabled={responsiveEnabled}
        onResponsiveToggle={handleResponsiveToggle}
        attributes={attributes}
        setAttributes={setAttributes}
        syncAttributeName={responsiveAttrName}
      >
        {(tabName) => renderRadiusControls(tabName)}
      </ResponsiveControlV2>
    </PanelBodyWithSync>
  );
};

export default IconFrameRadiusSettingV6;
