"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/hooks/border.js
var border_exports = {};
__export(border_exports, {
  BORDER_SUPPORT_KEY: () => BORDER_SUPPORT_KEY,
  BorderPanel: () => BorderPanel,
  SHADOW_SUPPORT_KEY: () => SHADOW_SUPPORT_KEY,
  default: () => border_default,
  getBorderClasses: () => getBorderClasses,
  getMultiOriginColor: () => getMultiOriginColor,
  hasBorderSupport: () => hasBorderSupport,
  hasShadowSupport: () => hasShadowSupport,
  removeBorderAttribute: () => removeBorderAttribute,
  useBorderPanelLabel: () => useBorderPanelLabel
});
module.exports = __toCommonJS(border_exports);
var import_clsx = __toESM(require("clsx"));
var import_blocks = require("@wordpress/blocks");
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_hooks = require("@wordpress/hooks");
var import_data = require("@wordpress/data");
var import_colors = require("../components/colors");
var import_inspector_controls = __toESM(require("../components/inspector-controls"));
var import_use_multiple_origin_colors_and_gradients = __toESM(require("../components/colors-gradients/use-multiple-origin-colors-and-gradients"));
var import_utils = require("./utils");
var import_global_styles = require("../components/global-styles");
var import_store = require("../store");
var import_i18n = require("@wordpress/i18n");
var import_jsx_runtime = require("react/jsx-runtime");
var BORDER_SUPPORT_KEY = "__experimentalBorder";
var SHADOW_SUPPORT_KEY = "shadow";
var getColorByProperty = (colors, property, value) => {
  let matchedColor;
  colors.some(
    (origin) => origin.colors.some((color) => {
      if (color[property] === value) {
        matchedColor = color;
        return true;
      }
      return false;
    })
  );
  return matchedColor;
};
var getMultiOriginColor = ({ colors, namedColor, customColor }) => {
  if (namedColor) {
    const colorObject2 = getColorByProperty(colors, "slug", namedColor);
    if (colorObject2) {
      return colorObject2;
    }
  }
  if (!customColor) {
    return { color: void 0 };
  }
  const colorObject = getColorByProperty(colors, "color", customColor);
  return colorObject ? colorObject : { color: customColor };
};
function getColorSlugFromVariable(value) {
  const namedColor = /var:preset\|color\|(.+)/.exec(value);
  if (namedColor && namedColor[1]) {
    return namedColor[1];
  }
  return null;
}
function styleToAttributes(style) {
  if ((0, import_components.__experimentalHasSplitBorders)(style?.border)) {
    return {
      style,
      borderColor: void 0
    };
  }
  const borderColorValue = style?.border?.color;
  const borderColorSlug = borderColorValue?.startsWith("var:preset|color|") ? borderColorValue.substring("var:preset|color|".length) : void 0;
  const updatedStyle = { ...style };
  updatedStyle.border = {
    ...updatedStyle.border,
    color: borderColorSlug ? void 0 : borderColorValue
  };
  return {
    style: (0, import_utils.cleanEmptyObject)(updatedStyle),
    borderColor: borderColorSlug
  };
}
function attributesToStyle(attributes) {
  if ((0, import_components.__experimentalHasSplitBorders)(attributes.style?.border)) {
    return attributes.style;
  }
  return {
    ...attributes.style,
    border: {
      ...attributes.style?.border,
      color: attributes.borderColor ? "var:preset|color|" + attributes.borderColor : attributes.style?.border?.color
    }
  };
}
function BordersInspectorControl({ label, children, resetAllFilter }) {
  const attributesResetAllFilter = (0, import_element.useCallback)(
    (attributes) => {
      const existingStyle = attributesToStyle(attributes);
      const updatedStyle = resetAllFilter(existingStyle);
      return {
        ...attributes,
        ...styleToAttributes(updatedStyle)
      };
    },
    [resetAllFilter]
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_inspector_controls.default,
    {
      group: "border",
      resetAllFilter: attributesResetAllFilter,
      label,
      children
    }
  );
}
function BorderPanel({ clientId, name, setAttributes, settings }) {
  const isEnabled = (0, import_global_styles.useHasBorderPanel)(settings);
  const { style, borderColor } = (0, import_data.useSelect)(
    (select) => {
      if (!isEnabled) {
        return {};
      }
      const { style: _style, borderColor: _borderColor } = select(import_store.store).getBlockAttributes(clientId) || {};
      return { style: _style, borderColor: _borderColor };
    },
    [clientId, isEnabled]
  );
  const value = (0, import_element.useMemo)(() => {
    return attributesToStyle({ style, borderColor });
  }, [style, borderColor]);
  const onChange = (newStyle) => {
    setAttributes(styleToAttributes(newStyle));
  };
  if (!isEnabled) {
    return null;
  }
  const defaultControls = {
    ...(0, import_blocks.getBlockSupport)(name, [
      BORDER_SUPPORT_KEY,
      "__experimentalDefaultControls"
    ]),
    ...(0, import_blocks.getBlockSupport)(name, [
      SHADOW_SUPPORT_KEY,
      "__experimentalDefaultControls"
    ])
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_global_styles.BorderPanel,
    {
      as: BordersInspectorControl,
      panelId: clientId,
      settings,
      value,
      onChange,
      defaultControls
    }
  );
}
function hasBorderSupport(blockName, feature = "any") {
  if (import_element.Platform.OS !== "web") {
    return false;
  }
  const support = (0, import_blocks.getBlockSupport)(blockName, BORDER_SUPPORT_KEY);
  if (support === true) {
    return true;
  }
  if (feature === "any") {
    return !!(support?.color || support?.radius || support?.width || support?.style);
  }
  return !!support?.[feature];
}
function hasShadowSupport(blockName) {
  return (0, import_blocks.hasBlockSupport)(blockName, SHADOW_SUPPORT_KEY);
}
function useBorderPanelLabel({
  blockName,
  hasBorderControl,
  hasShadowControl
} = {}) {
  const settings = (0, import_utils.useBlockSettings)(blockName);
  const controls = (0, import_global_styles.useHasBorderPanelControls)(settings);
  if (!hasBorderControl && !hasShadowControl && blockName) {
    hasBorderControl = controls?.hasBorderColor || controls?.hasBorderStyle || controls?.hasBorderWidth || controls?.hasBorderRadius;
    hasShadowControl = controls?.hasShadow;
  }
  if (hasBorderControl && hasShadowControl) {
    return (0, import_i18n.__)("Border & Shadow");
  }
  if (hasShadowControl) {
    return (0, import_i18n.__)("Shadow");
  }
  return (0, import_i18n.__)("Border");
}
function removeBorderAttribute(style, attribute) {
  return (0, import_utils.cleanEmptyObject)({
    ...style,
    border: {
      ...style?.border,
      [attribute]: void 0
    }
  });
}
function addAttributes(settings) {
  if (!hasBorderSupport(settings, "color")) {
    return settings;
  }
  if (settings.attributes.borderColor) {
    return settings;
  }
  return {
    ...settings,
    attributes: {
      ...settings.attributes,
      borderColor: {
        type: "string"
      }
    }
  };
}
function addSaveProps(props, blockNameOrType, attributes) {
  if (!hasBorderSupport(blockNameOrType, "color") || (0, import_utils.shouldSkipSerialization)(blockNameOrType, BORDER_SUPPORT_KEY, "color")) {
    return props;
  }
  const borderClasses = getBorderClasses(attributes);
  const newClassName = (0, import_clsx.default)(props.className, borderClasses);
  props.className = newClassName ? newClassName : void 0;
  return props;
}
function getBorderClasses(attributes) {
  const { borderColor, style } = attributes;
  const borderColorClass = (0, import_colors.getColorClassName)("border-color", borderColor);
  return (0, import_clsx.default)({
    "has-border-color": borderColor || style?.border?.color,
    [borderColorClass]: !!borderColorClass
  });
}
function useBlockProps({ name, borderColor, style }) {
  const { colors } = (0, import_use_multiple_origin_colors_and_gradients.default)();
  if (!hasBorderSupport(name, "color") || (0, import_utils.shouldSkipSerialization)(name, BORDER_SUPPORT_KEY, "color")) {
    return {};
  }
  const { color: borderColorValue } = getMultiOriginColor({
    colors,
    namedColor: borderColor
  });
  const { color: borderTopColor } = getMultiOriginColor({
    colors,
    namedColor: getColorSlugFromVariable(style?.border?.top?.color)
  });
  const { color: borderRightColor } = getMultiOriginColor({
    colors,
    namedColor: getColorSlugFromVariable(style?.border?.right?.color)
  });
  const { color: borderBottomColor } = getMultiOriginColor({
    colors,
    namedColor: getColorSlugFromVariable(style?.border?.bottom?.color)
  });
  const { color: borderLeftColor } = getMultiOriginColor({
    colors,
    namedColor: getColorSlugFromVariable(style?.border?.left?.color)
  });
  const extraStyles = {
    borderTopColor: borderTopColor || borderColorValue,
    borderRightColor: borderRightColor || borderColorValue,
    borderBottomColor: borderBottomColor || borderColorValue,
    borderLeftColor: borderLeftColor || borderColorValue
  };
  return addSaveProps(
    { style: (0, import_utils.cleanEmptyObject)(extraStyles) || {} },
    name,
    { borderColor, style }
  );
}
var border_default = {
  useBlockProps,
  addSaveProps,
  attributeKeys: ["borderColor", "style"],
  hasSupport(name) {
    return hasBorderSupport(name, "color");
  }
};
(0, import_hooks.addFilter)(
  "blocks.registerBlockType",
  "core/border/addAttributes",
  addAttributes
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BORDER_SUPPORT_KEY,
  BorderPanel,
  SHADOW_SUPPORT_KEY,
  getBorderClasses,
  getMultiOriginColor,
  hasBorderSupport,
  hasShadowSupport,
  removeBorderAttribute,
  useBorderPanelLabel
});
//# sourceMappingURL=border.js.map
