"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/gradients/use-gradient.js
var use_gradient_exports = {};
__export(use_gradient_exports, {
  __experimentalGetGradientClass: () => __experimentalGetGradientClass,
  __experimentalGetGradientObjectByGradientValue: () => __experimentalGetGradientObjectByGradientValue,
  __experimentalUseGradient: () => useGradient,
  getGradientSlugByValue: () => getGradientSlugByValue,
  getGradientValueBySlug: () => getGradientValueBySlug
});
module.exports = __toCommonJS(use_gradient_exports);
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_block_edit = require("../block-edit/index.cjs");
var import_use_settings = require("../use-settings/index.cjs");
var import_store = require("../../store/index.cjs");
function __experimentalGetGradientClass(gradientSlug) {
  if (!gradientSlug) {
    return void 0;
  }
  return `has-${gradientSlug}-gradient-background`;
}
function getGradientValueBySlug(gradients, slug) {
  const gradient = gradients?.find((g) => g.slug === slug);
  return gradient && gradient.gradient;
}
function __experimentalGetGradientObjectByGradientValue(gradients, value) {
  const gradient = gradients?.find((g) => g.gradient === value);
  return gradient;
}
function getGradientSlugByValue(gradients, value) {
  const gradient = __experimentalGetGradientObjectByGradientValue(
    gradients,
    value
  );
  return gradient && gradient.slug;
}
function useGradient({
  gradientAttribute = "gradient",
  customGradientAttribute = "customGradient"
} = {}) {
  const { clientId } = (0, import_block_edit.useBlockEditContext)();
  const [
    userGradientPalette,
    themeGradientPalette,
    defaultGradientPalette
  ] = (0, import_use_settings.useSettings)(
    "color.gradients.custom",
    "color.gradients.theme",
    "color.gradients.default"
  );
  const allGradients = (0, import_element.useMemo)(
    () => [
      ...userGradientPalette || [],
      ...themeGradientPalette || [],
      ...defaultGradientPalette || []
    ],
    [userGradientPalette, themeGradientPalette, defaultGradientPalette]
  );
  const { gradient, customGradient } = (0, import_data.useSelect)(
    (select) => {
      const { getBlockAttributes } = select(import_store.store);
      const attributes = getBlockAttributes(clientId) || {};
      return {
        customGradient: attributes[customGradientAttribute],
        gradient: attributes[gradientAttribute]
      };
    },
    [clientId, gradientAttribute, customGradientAttribute]
  );
  const { updateBlockAttributes } = (0, import_data.useDispatch)(import_store.store);
  const setGradient = (0, import_element.useCallback)(
    (newGradientValue) => {
      const slug = getGradientSlugByValue(
        allGradients,
        newGradientValue
      );
      if (slug) {
        updateBlockAttributes(clientId, {
          [gradientAttribute]: slug,
          [customGradientAttribute]: void 0
        });
        return;
      }
      updateBlockAttributes(clientId, {
        [gradientAttribute]: void 0,
        [customGradientAttribute]: newGradientValue
      });
    },
    [allGradients, clientId, updateBlockAttributes]
  );
  const gradientClass = __experimentalGetGradientClass(gradient);
  let gradientValue;
  if (gradient) {
    gradientValue = getGradientValueBySlug(allGradients, gradient);
  } else {
    gradientValue = customGradient;
  }
  return { gradientClass, gradientValue, setGradient };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  __experimentalGetGradientClass,
  __experimentalGetGradientObjectByGradientValue,
  __experimentalUseGradient,
  getGradientSlugByValue,
  getGradientValueBySlug
});
//# sourceMappingURL=use-gradient.cjs.map
