"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);

// packages/block-editor/src/components/editor-styles/index.js
var editor_styles_exports = {};
__export(editor_styles_exports, {
  default: () => editor_styles_default
});
module.exports = __toCommonJS(editor_styles_exports);
var import_colord = require("colord");
var import_names = __toESM(require("colord/plugins/names"));
var import_a11y = __toESM(require("colord/plugins/a11y"));
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_transform_styles = __toESM(require("../../utils/transform-styles"));
var import_store = require("../../store");
var import_lock_unlock = require("../../lock-unlock");
var import_jsx_runtime = require("react/jsx-runtime");
(0, import_colord.extend)([import_names.default, import_a11y.default]);
function useDarkThemeBodyClassName(styles, scope) {
  return (0, import_element.useCallback)(
    (node) => {
      if (!node) {
        return;
      }
      const { ownerDocument } = node;
      const { defaultView, body } = ownerDocument;
      const canvas = scope ? ownerDocument.querySelector(scope) : body;
      let backgroundColor;
      if (!canvas) {
        const tempCanvas = ownerDocument.createElement("div");
        tempCanvas.classList.add("editor-styles-wrapper");
        body.appendChild(tempCanvas);
        backgroundColor = defaultView?.getComputedStyle(tempCanvas, null).getPropertyValue("background-color");
        body.removeChild(tempCanvas);
      } else {
        backgroundColor = defaultView?.getComputedStyle(canvas, null).getPropertyValue("background-color");
      }
      const colordBackgroundColor = (0, import_colord.colord)(backgroundColor);
      if (colordBackgroundColor.luminance() > 0.5 || colordBackgroundColor.alpha() === 0) {
        body.classList.remove("is-dark-theme");
      } else {
        body.classList.add("is-dark-theme");
      }
    },
    [styles, scope]
  );
}
function EditorStyles({ styles, scope, transformOptions }) {
  const overrides = (0, import_data.useSelect)(
    (select) => (0, import_lock_unlock.unlock)(select(import_store.store)).getStyleOverrides(),
    []
  );
  const [transformedStyles, transformedSvgs] = (0, import_element.useMemo)(() => {
    const _styles = Object.values(styles ?? []);
    for (const [id, override] of overrides) {
      const index = _styles.findIndex(({ id: _id }) => id === _id);
      const overrideWithId = { ...override, id };
      if (index === -1) {
        _styles.push(overrideWithId);
      } else {
        _styles[index] = overrideWithId;
      }
    }
    return [
      (0, import_transform_styles.default)(
        _styles.filter((style) => style?.css),
        scope,
        transformOptions
      ),
      _styles.filter((style) => style.__unstableType === "svgs").map((style) => style.assets).join("")
    ];
  }, [styles, overrides, scope, transformOptions]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      "style",
      {
        ref: useDarkThemeBodyClassName(transformedStyles, scope)
      }
    ),
    transformedStyles.map((css, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { children: css }, index)),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.SVG,
      {
        xmlns: "http://www.w3.org/2000/svg",
        viewBox: "0 0 0 0",
        width: "0",
        height: "0",
        role: "none",
        style: {
          visibility: "hidden",
          position: "absolute",
          left: "-9999px",
          overflow: "hidden"
        },
        dangerouslySetInnerHTML: { __html: transformedSvgs }
      }
    )
  ] });
}
var editor_styles_default = (0, import_element.memo)(EditorStyles);
//# sourceMappingURL=index.js.map
