"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/global-styles/border-panel.js
var border_panel_exports = {};
__export(border_panel_exports, {
  default: () => BorderPanel,
  useHasBorderPanel: () => useHasBorderPanel,
  useHasBorderPanelControls: () => useHasBorderPanelControls
});
module.exports = __toCommonJS(border_panel_exports);
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_global_styles_engine = require("@wordpress/global-styles-engine");
var import_border_radius_control = __toESM(require("../border-radius-control/index.cjs"));
var import_hooks = require("./hooks.cjs");
var import_utils = require("./utils.cjs");
var import_object = require("../../utils/object.cjs");
var import_border = require("../../hooks/border.cjs");
var import_shadow_panel_components = require("./shadow-panel-components.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function useHasBorderPanel(settings) {
  const controls = Object.values(useHasBorderPanelControls(settings));
  return controls.some(Boolean);
}
function useHasBorderPanelControls(settings) {
  const controls = {
    hasBorderColor: useHasBorderColorControl(settings),
    hasBorderRadius: useHasBorderRadiusControl(settings),
    hasBorderStyle: useHasBorderStyleControl(settings),
    hasBorderWidth: useHasBorderWidthControl(settings),
    hasShadow: useHasShadowControl(settings)
  };
  return controls;
}
function useHasBorderColorControl(settings) {
  return settings?.border?.color;
}
function useHasBorderRadiusControl(settings) {
  return settings?.border?.radius;
}
function useHasBorderStyleControl(settings) {
  return settings?.border?.style;
}
function useHasBorderWidthControl(settings) {
  return settings?.border?.width;
}
function useHasShadowControl(settings) {
  const shadows = (0, import_shadow_panel_components.useShadowPresets)(settings);
  return !!settings?.shadow && shadows.length > 0;
}
function BorderToolsPanel({
  resetAllFilter,
  onChange,
  value,
  panelId,
  children,
  label
}) {
  const dropdownMenuProps = (0, import_utils.useToolsPanelDropdownMenuProps)();
  const resetAll = () => {
    const updatedValue = resetAllFilter(value);
    onChange(updatedValue);
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.__experimentalToolsPanel,
    {
      label,
      resetAll,
      panelId,
      dropdownMenuProps,
      children
    }
  );
}
var DEFAULT_CONTROLS = {
  radius: true,
  color: true,
  width: true,
  shadow: true
};
function BorderPanel({
  as: Wrapper = BorderToolsPanel,
  value,
  onChange,
  inheritedValue = value,
  settings,
  panelId,
  name,
  defaultControls = DEFAULT_CONTROLS
}) {
  const colors = (0, import_hooks.useColorsPerOrigin)(settings);
  const areCustomSolidsEnabled = settings?.color?.custom;
  const decodeValue = (0, import_element.useCallback)(
    (rawValue) => (0, import_global_styles_engine.getValueFromVariable)({ settings }, "", rawValue),
    [settings]
  );
  const encodeColorValue = (colorValue) => {
    const allColors = colors.flatMap(
      ({ colors: originColors }) => originColors
    );
    const colorObject = allColors.find(
      ({ color }) => color === colorValue
    );
    return colorObject ? "var:preset|color|" + colorObject.slug : colorValue;
  };
  const border = (0, import_element.useMemo)(() => {
    if ((0, import_components.__experimentalHasSplitBorders)(inheritedValue?.border)) {
      const borderValue = { ...inheritedValue?.border };
      ["top", "right", "bottom", "left"].forEach((side) => {
        borderValue[side] = {
          ...borderValue[side],
          color: decodeValue(borderValue[side]?.color)
        };
      });
      return borderValue;
    }
    return {
      ...inheritedValue?.border,
      color: inheritedValue?.border?.color ? decodeValue(inheritedValue?.border?.color) : void 0
    };
  }, [inheritedValue?.border, decodeValue]);
  const setBorder = (newBorder) => onChange({ ...value, border: newBorder });
  const showBorderColor = useHasBorderColorControl(settings);
  const showBorderStyle = useHasBorderStyleControl(settings);
  const showBorderWidth = useHasBorderWidthControl(settings);
  const showBorderRadius = useHasBorderRadiusControl(settings);
  const borderRadiusValues = (0, import_element.useMemo)(() => {
    if (typeof inheritedValue?.border?.radius !== "object") {
      return decodeValue(inheritedValue?.border?.radius);
    }
    return {
      topLeft: decodeValue(inheritedValue?.border?.radius?.topLeft),
      topRight: decodeValue(inheritedValue?.border?.radius?.topRight),
      bottomLeft: decodeValue(
        inheritedValue?.border?.radius?.bottomLeft
      ),
      bottomRight: decodeValue(
        inheritedValue?.border?.radius?.bottomRight
      )
    };
  }, [inheritedValue?.border?.radius, decodeValue]);
  const setBorderRadius = (newBorderRadius) => setBorder({ ...border, radius: newBorderRadius });
  const hasBorderRadius = () => {
    const borderValues = value?.border?.radius;
    if (typeof borderValues === "object") {
      return Object.entries(borderValues).some(Boolean);
    }
    return !!borderValues;
  };
  const hasShadowControl = useHasShadowControl(settings);
  const shadow = decodeValue(inheritedValue?.shadow);
  const shadowPresets = settings?.shadow?.presets ?? {};
  const mergedShadowPresets = shadowPresets.custom ?? shadowPresets.theme ?? shadowPresets.default ?? [];
  const setShadow = (newValue) => {
    const slug = mergedShadowPresets?.find(
      ({ shadow: shadowName }) => shadowName === newValue
    )?.slug;
    onChange(
      (0, import_object.setImmutably)(
        value,
        ["shadow"],
        slug ? `var:preset|shadow|${slug}` : newValue || void 0
      )
    );
  };
  const hasShadow = () => !!value?.shadow;
  const resetShadow = () => setShadow(void 0);
  const resetBorder = () => {
    if (hasBorderRadius()) {
      return setBorder({ radius: value?.border?.radius });
    }
    setBorder(void 0);
  };
  const onBorderChange = (newBorder) => {
    const updatedBorder = { ...newBorder };
    if ((0, import_components.__experimentalHasSplitBorders)(updatedBorder)) {
      ["top", "right", "bottom", "left"].forEach((side) => {
        if (updatedBorder[side]) {
          updatedBorder[side] = {
            ...updatedBorder[side],
            color: encodeColorValue(updatedBorder[side]?.color)
          };
        }
      });
    } else if (updatedBorder) {
      updatedBorder.color = encodeColorValue(updatedBorder.color);
    }
    setBorder({ radius: border?.radius, ...updatedBorder });
  };
  const resetAllFilter = (0, import_element.useCallback)((previousValue) => {
    return {
      ...previousValue,
      border: void 0,
      shadow: void 0
    };
  }, []);
  const showBorderByDefault = defaultControls?.color || defaultControls?.width;
  const hasBorderControl = showBorderColor || showBorderStyle || showBorderWidth || showBorderRadius;
  const label = (0, import_border.useBorderPanelLabel)({
    blockName: name,
    hasShadowControl,
    hasBorderControl
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    Wrapper,
    {
      resetAllFilter,
      value,
      onChange,
      panelId,
      label,
      children: [
        (showBorderWidth || showBorderColor) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.__experimentalToolsPanelItem,
          {
            hasValue: () => (0, import_components.__experimentalIsDefinedBorder)(value?.border),
            label: (0, import_i18n.__)("Border"),
            onDeselect: () => resetBorder(),
            isShownByDefault: showBorderByDefault,
            panelId,
            children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_components.BorderBoxControl,
              {
                colors,
                disableCustomColors: !areCustomSolidsEnabled,
                enableAlpha: true,
                enableStyle: showBorderStyle,
                onChange: onBorderChange,
                popoverOffset: 40,
                popoverPlacement: "left-start",
                value: border,
                __experimentalIsRenderedInSidebar: true,
                size: "__unstable-large",
                hideLabelFromVision: !hasShadowControl,
                label: (0, import_i18n.__)("Border")
              }
            )
          }
        ),
        showBorderRadius && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.__experimentalToolsPanelItem,
          {
            hasValue: hasBorderRadius,
            label: (0, import_i18n.__)("Radius"),
            onDeselect: () => setBorderRadius(void 0),
            isShownByDefault: defaultControls.radius,
            panelId,
            children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_border_radius_control.default,
              {
                presets: settings?.border?.radiusSizes,
                values: borderRadiusValues,
                onChange: (newValue) => {
                  setBorderRadius(newValue || void 0);
                }
              }
            )
          }
        ),
        hasShadowControl && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
          import_components.__experimentalToolsPanelItem,
          {
            label: (0, import_i18n.__)("Shadow"),
            hasValue: hasShadow,
            onDeselect: resetShadow,
            isShownByDefault: defaultControls.shadow,
            panelId,
            children: [
              hasBorderControl ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.BaseControl.VisualLabel, { as: "legend", children: (0, import_i18n.__)("Shadow") }) : null,
              /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_shadow_panel_components.ShadowPopover,
                {
                  shadow,
                  onShadowChange: setShadow,
                  settings
                }
              )
            ]
          }
        )
      ]
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useHasBorderPanel,
  useHasBorderPanelControls
});
//# sourceMappingURL=border-panel.cjs.map
