"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/components/src/color-palette/utils.ts
var utils_exports = {};
__export(utils_exports, {
  extractColorNameFromCurrentValue: () => extractColorNameFromCurrentValue,
  isMultiplePaletteArray: () => isMultiplePaletteArray,
  isMultiplePaletteObject: () => isMultiplePaletteObject,
  normalizeColorValue: () => normalizeColorValue
});
module.exports = __toCommonJS(utils_exports);
var import_colord = require("colord");
var import_names = __toESM(require("colord/plugins/names"), 1);
var import_a11y = __toESM(require("colord/plugins/a11y"), 1);
var import_i18n = require("@wordpress/i18n");
(0, import_colord.extend)([import_names.default, import_a11y.default]);
var isSimpleCSSColor = (value) => {
  const valueIsCssVariable = /var\(/.test(value !== null && value !== void 0 ? value : "");
  const valueIsColorMix = /color-mix\(/.test(value !== null && value !== void 0 ? value : "");
  return !valueIsCssVariable && !valueIsColorMix;
};
var extractColorNameFromCurrentValue = (currentValue, colors = [], showMultiplePalettes = false) => {
  if (!currentValue) {
    return "";
  }
  const currentValueIsSimpleColor = currentValue ? isSimpleCSSColor(currentValue) : false;
  const normalizedCurrentValue = currentValueIsSimpleColor ? (0, import_colord.colord)(currentValue).toHex() : currentValue;
  const colorPalettes = showMultiplePalettes ? colors : [{
    colors
  }];
  for (const {
    colors: paletteColors
  } of colorPalettes) {
    for (const {
      name: colorName,
      color: colorValue
    } of paletteColors) {
      const normalizedColorValue = currentValueIsSimpleColor ? (0, import_colord.colord)(colorValue).toHex() : colorValue;
      if (normalizedCurrentValue === normalizedColorValue) {
        return colorName;
      }
    }
  }
  return (0, import_i18n.__)("Custom");
};
var isMultiplePaletteObject = (obj) => Array.isArray(obj.colors) && !("color" in obj);
var isMultiplePaletteArray = (arr) => {
  return arr.length > 0 && arr.every((colorObj) => isMultiplePaletteObject(colorObj));
};
var normalizeColorValue = (value, element) => {
  if (!value || !element || isSimpleCSSColor(value)) {
    return value;
  }
  const {
    ownerDocument
  } = element;
  const {
    defaultView
  } = ownerDocument;
  const computedBackgroundColor = defaultView?.getComputedStyle(element).backgroundColor;
  return computedBackgroundColor ? (0, import_colord.colord)(computedBackgroundColor).toHex() : value;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  extractColorNameFromCurrentValue,
  isMultiplePaletteArray,
  isMultiplePaletteObject,
  normalizeColorValue
});
//# sourceMappingURL=utils.cjs.map
