import { useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { useContext, useMemo } from "@wordpress/element";
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
import { privateApis as editorPrivateApis } from "@wordpress/editor";
import { __ } from "@wordpress/i18n";
import { unlock } from "../../lock-unlock";
const EMPTY_ARRAY = [];
const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock(
  blockEditorPrivateApis
);
const { mergeBaseAndUserConfigs } = unlock(editorPrivateApis);
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 === __("Default") || // Always preserve the default variation.
  Object.keys(settings).length > 0 || Object.keys(styles).length > 0;
}
function useCurrentMergeThemeStyleVariationsWithUserConfig(properties = []) {
  const { variationsFromTheme } = useSelect((select) => {
    const _variationsFromTheme = select(
      coreStore
    ).__experimentalGetCurrentThemeGlobalStylesVariations();
    return {
      variationsFromTheme: _variationsFromTheme || EMPTY_ARRAY
    };
  }, []);
  const { user: userVariation } = useContext(GlobalStylesContext);
  const propertiesAsString = properties.toString();
  return useMemo(() => {
    const clonedUserVariation = structuredClone(userVariation);
    const userVariationWithoutProperties = removePropertiesFromObject(
      clonedUserVariation,
      properties
    );
    userVariationWithoutProperties.title = __("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);
}
export {
  filterObjectByProperties,
  isVariationWithProperties,
  removePropertiesFromObject,
  useCurrentMergeThemeStyleVariationsWithUserConfig
};
//# sourceMappingURL=use-theme-style-variations-by-property.js.map
