"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);
var variation_exports = {};
__export(variation_exports, {
  default: () => Variation
});
module.exports = __toCommonJS(variation_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_clsx = __toESM(require("clsx"));
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_keycodes = require("@wordpress/keycodes");
var import_i18n = require("@wordpress/i18n");
var import_block_editor = require("@wordpress/block-editor");
var import_editor = require("@wordpress/editor");
var import_use_theme_style_variations_by_property = require("../../../hooks/use-theme-style-variations/use-theme-style-variations-by-property");
var import_lock_unlock = require("../../../lock-unlock");
const { mergeBaseAndUserConfigs } = (0, import_lock_unlock.unlock)(import_editor.privateApis);
const { GlobalStylesContext, areGlobalStyleConfigsEqual } = (0, import_lock_unlock.unlock)(
  import_block_editor.privateApis
);
function Variation({
  variation,
  children,
  isPill,
  properties,
  showTooltip
}) {
  const [isFocused, setIsFocused] = (0, import_element.useState)(false);
  const { base, user, setUserConfig } = (0, import_element.useContext)(GlobalStylesContext);
  const context = (0, import_element.useMemo)(() => {
    let merged = mergeBaseAndUserConfigs(base, variation);
    if (properties) {
      merged = (0, import_use_theme_style_variations_by_property.filterObjectByProperties)(merged, properties);
    }
    return {
      user: variation,
      base,
      merged,
      setUserConfig: () => {
      }
    };
  }, [variation, base, properties]);
  const selectVariation = () => setUserConfig(variation);
  const selectOnEnter = (event) => {
    if (event.keyCode === import_keycodes.ENTER) {
      event.preventDefault();
      selectVariation();
    }
  };
  const isActive = (0, import_element.useMemo)(
    () => areGlobalStyleConfigsEqual(user, variation),
    [user, variation]
  );
  let label = variation?.title;
  if (variation?.description) {
    label = (0, import_i18n.sprintf)(
      /* translators: 1: variation title. 2: variation description. */
      (0, import_i18n._x)("%1$s (%2$s)", "variation label"),
      variation?.title,
      variation?.description
    );
  }
  const content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "div",
    {
      className: (0, import_clsx.default)("edit-site-global-styles-variations_item", {
        "is-active": isActive
      }),
      role: "button",
      onClick: selectVariation,
      onKeyDown: selectOnEnter,
      tabIndex: "0",
      "aria-label": label,
      "aria-current": isActive,
      onFocus: () => setIsFocused(true),
      onBlur: () => setIsFocused(false),
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        "div",
        {
          className: (0, import_clsx.default)(
            "edit-site-global-styles-variations_item-preview",
            { "is-pill": isPill }
          ),
          children: children(isFocused)
        }
      )
    }
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(GlobalStylesContext.Provider, { value: context, children: showTooltip ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Tooltip, { text: variation?.title, children: content }) : content });
}
//# sourceMappingURL=variation.js.map
