"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);
var use_theme_style_variations_by_property_exports = {};
__export(use_theme_style_variations_by_property_exports, {
  filterObjectByProperties: () => filterObjectByProperties,
  isVariationWithProperties: () => isVariationWithProperties,
  removePropertiesFromObject: () => removePropertiesFromObject,
  useCurrentMergeThemeStyleVariationsWithUserConfig: () => useCurrentMergeThemeStyleVariationsWithUserConfig
});
module.exports = __toCommonJS(use_theme_style_variations_by_property_exports);
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_element = require("@wordpress/element");
var import_block_editor = require("@wordpress/block-editor");
var import_editor = require("@wordpress/editor");
var import_i18n = require("@wordpress/i18n");
var import_lock_unlock = require("../../lock-unlock");
const EMPTY_ARRAY = [];
const { GlobalStylesContext, areGlobalStyleConfigsEqual } = (0, import_lock_unlock.unlock)(
  import_block_editor.privateApis
);
const { mergeBaseAndUserConfigs } = (0, import_lock_unlock.unlock)(import_editor.privateApis);
function removePropertiesFromObject(object, properties) {
  if (!properties?.length) {
    return object;
  }
  if (typeof object !== "object" || !object || !Object.keys(object).length) {
    return object;
  }
  for (const key in object) {
    if (properties.includes(key)) {
      delete object[key];
    } else if (typeof object[key] === "object") {
      removePropertiesFromObject(object[key], properties);
    }
  }
  return object;
}
function hasThemeVariation({ title, settings, styles }) {
  return title === (0, import_i18n.__)("Default") || // Always preserve the default variation.
  Object.keys(settings).length > 0 || Object.keys(styles).length > 0;
}
function useCurrentMergeThemeStyleVariationsWithUserConfig(properties = []) {
  const { variationsFromTheme } = (0, import_data.useSelect)((select) => {
    const _variationsFromTheme = select(
      import_core_data.store
    ).__experimentalGetCurrentThemeGlobalStylesVariations();
    return {
      variationsFromTheme: _variationsFromTheme || EMPTY_ARRAY
    };
  }, []);
  const { user: userVariation } = (0, import_element.useContext)(GlobalStylesContext);
  const propertiesAsString = properties.toString();
  return (0, import_element.useMemo)(() => {
    const clonedUserVariation = structuredClone(userVariation);
    const userVariationWithoutProperties = removePropertiesFromObject(
      clonedUserVariation,
      properties
    );
    userVariationWithoutProperties.title = (0, import_i18n.__)("Default");
    const variationsWithPropertiesAndBase = variationsFromTheme.filter((variation) => {
      return isVariationWithProperties(variation, properties);
    }).map((variation) => {
      return mergeBaseAndUserConfigs(
        userVariationWithoutProperties,
        variation
      );
    });
    const variationsByProperties = [
      userVariationWithoutProperties,
      ...variationsWithPropertiesAndBase
    ];
    return variationsByProperties?.length ? variationsByProperties.filter(hasThemeVariation) : [];
  }, [propertiesAsString, userVariation, variationsFromTheme]);
}
const filterObjectByProperties = (object, properties) => {
  if (!object || !properties?.length) {
    return {};
  }
  const newObject = {};
  Object.keys(object).forEach((key) => {
    if (properties.includes(key)) {
      newObject[key] = object[key];
    } else if (typeof object[key] === "object") {
      const newFilter = filterObjectByProperties(
        object[key],
        properties
      );
      if (Object.keys(newFilter).length) {
        newObject[key] = newFilter;
      }
    }
  });
  return newObject;
};
function isVariationWithProperties(variation, properties) {
  const variationWithProperties = filterObjectByProperties(
    structuredClone(variation),
    properties
  );
  return areGlobalStyleConfigsEqual(variationWithProperties, variation);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  filterObjectByProperties,
  isVariationWithProperties,
  removePropertiesFromObject,
  useCurrentMergeThemeStyleVariationsWithUserConfig
});
//# sourceMappingURL=use-theme-style-variations-by-property.js.map
