"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/border-radius-control/utils.js
var utils_exports = {};
__export(utils_exports, {
  convertPresetsToCustomValues: () => convertPresetsToCustomValues,
  getAllUnit: () => getAllUnit,
  getAllValue: () => getAllValue,
  getCustomValueFromPreset: () => getCustomValueFromPreset,
  getPresetSlug: () => getPresetSlug,
  getPresetValueFromControlValue: () => getPresetValueFromControlValue,
  getPresetValueFromCustomValue: () => getPresetValueFromCustomValue,
  getSliderValueFromPreset: () => getSliderValueFromPreset,
  hasDefinedValues: () => hasDefinedValues,
  hasMixedValues: () => hasMixedValues,
  isValuePreset: () => isValuePreset,
  mode: () => mode
});
module.exports = __toCommonJS(utils_exports);
var import_components = require("@wordpress/components");
function mode(inputArray) {
  const arr = [...inputArray];
  return arr.sort(
    (a, b) => inputArray.filter((v) => v === b).length - inputArray.filter((v) => v === a).length
  ).shift();
}
function getAllUnit(selectedUnits = {}) {
  const { flat, ...cornerUnits } = selectedUnits;
  return flat || mode(Object.values(cornerUnits).filter(Boolean)) || "px";
}
function getAllValue(values = {}) {
  if (typeof values === "string") {
    return values;
  }
  const parsedQuantitiesAndUnits = Object.values(values).map((value2) => {
    const newValue = (0, import_components.__experimentalParseQuantityAndUnitFromRawValue)(value2);
    if (typeof value2 === "string" && newValue[0] === void 0) {
      return [value2, ""];
    }
    return newValue;
  });
  const allValues = parsedQuantitiesAndUnits.map(
    (value2) => value2[0] ?? ""
  );
  const allUnits = parsedQuantitiesAndUnits.map((value2) => value2[1]);
  const value = allValues.every((v) => v === allValues[0]) ? allValues[0] : "";
  const unit = mode(allUnits);
  const allValue = value === 0 || value ? `${value}${unit || ""}` : void 0;
  return allValue;
}
function hasMixedValues(values = {}) {
  if (typeof values === "string") {
    return false;
  }
  if (!values || typeof values !== "object") {
    return false;
  }
  const cornerValues = Object.values(values);
  if (cornerValues.length === 0) {
    return false;
  }
  const firstValue = cornerValues[0];
  const allSame = cornerValues.every((value) => value === firstValue);
  return !allSame;
}
function hasDefinedValues(values) {
  if (!values) {
    return false;
  }
  if (typeof values === "string") {
    return true;
  }
  const filteredValues = Object.values(values).filter((value) => {
    return !!value || value === 0;
  });
  return !!filteredValues.length;
}
function isValuePreset(value) {
  if (!value?.includes) {
    return false;
  }
  return value === "0" || value.includes("var:preset|border-radius|");
}
function getPresetSlug(value) {
  if (!value) {
    return;
  }
  if (value === "0" || value === "default") {
    return value;
  }
  const slug = value.match(/var:preset\|border-radius\|(.+)/);
  return slug ? slug[1] : void 0;
}
function getSliderValueFromPreset(presetValue, presets) {
  if (presetValue === void 0) {
    return 0;
  }
  const slug = parseFloat(presetValue, 10) === 0 ? "0" : getPresetSlug(presetValue);
  const sliderValue = presets.findIndex((size) => {
    return String(size.slug) === slug;
  });
  return sliderValue !== -1 ? sliderValue : NaN;
}
function getCustomValueFromPreset(value, presets) {
  if (!isValuePreset(value)) {
    return value;
  }
  const slug = parseFloat(value, 10) === 0 ? "0" : getPresetSlug(value);
  const radiusSize = presets.find((size) => String(size.slug) === slug);
  return radiusSize?.size;
}
function getPresetValueFromControlValue(controlValue, controlType, presets) {
  const size = parseInt(controlValue, 10);
  if (controlType === "selectList") {
    if (size === 0) {
      return void 0;
    }
  } else if (size === 0) {
    return "0";
  }
  return `var:preset|border-radius|${presets[controlValue]?.slug}`;
}
function getPresetValueFromCustomValue(value, presets) {
  if (!value || isValuePreset(value) || value === "0") {
    return value;
  }
  const spacingMatch = presets.find(
    (size) => String(size.size) === String(value)
  );
  if (spacingMatch?.slug) {
    return `var:preset|border-radius|${spacingMatch.slug}`;
  }
  return value;
}
function convertPresetsToCustomValues(values, presets) {
  if (!values || typeof values !== "object") {
    return values;
  }
  const converted = {};
  Object.keys(values).forEach((key) => {
    const value = values[key];
    if (isValuePreset(value)) {
      const customValue = getCustomValueFromPreset(value, presets);
      converted[key] = customValue !== void 0 ? customValue : value;
    } else {
      converted[key] = value;
    }
  });
  return converted;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  convertPresetsToCustomValues,
  getAllUnit,
  getAllValue,
  getCustomValueFromPreset,
  getPresetSlug,
  getPresetValueFromControlValue,
  getPresetValueFromCustomValue,
  getSliderValueFromPreset,
  hasDefinedValues,
  hasMixedValues,
  isValuePreset,
  mode
});
//# sourceMappingURL=utils.js.map
