"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 responsive_exports = {};
__export(responsive_exports, {
  normalizeResponsiveValue: () => normalizeResponsiveValue,
  responsiveShallowEqual: () => responsiveShallowEqual,
  setResponsiveValue: () => setResponsiveValue
});
module.exports = __toCommonJS(responsive_exports);
var import_slate = require("slate");
var import_selectors = require("../selectors");
var import_shallowEqual = __toESM(require("../../utils/shallowEqual"));
function setResponsiveValue(editor, key, value, options) {
  import_slate.Editor.withoutNormalizing(editor, () => {
    const at = (0, import_selectors.getSelection)(editor);
    if (!at)
      return;
    const atRef = import_slate.Editor.rangeRef(editor, at);
    if (atRef.current) {
      import_slate.Transforms.setNodes(
        editor,
        {
          slice: true
        },
        {
          at: atRef.current,
          match: options.match,
          split: options.split
        }
      );
    }
    if (atRef.current) {
      const nodesToUpdate = Array.from(
        import_slate.Editor.nodes(editor, {
          at: atRef.current,
          match: (node, path) => options.match(node, path) && node.slice === true
        })
      );
      for (const [, path] of nodesToUpdate) {
        import_slate.Transforms.setNodes(
          editor,
          {
            [key]: value
          },
          { at: path }
        );
      }
    }
    atRef.unref();
  });
}
function normalizeResponsiveValue(editor, key, options) {
  return (entry) => {
    const [node, path] = entry;
    if (!options.match(node, path)) {
      return false;
    }
    const responsiveValue = node?.[key];
    if (Array.isArray(responsiveValue) && responsiveValue.length === 0) {
      import_slate.Transforms.unsetNodes(editor, key, { at: path });
      return true;
    }
    if (node?.slice != null) {
      import_slate.Transforms.unsetNodes(editor, "slice", { at: path });
      return true;
    }
    return false;
  };
}
function responsiveShallowEqual(a = [], b = []) {
  const aObject = a.reduce(
    (acc, curr) => ({
      ...acc,
      [curr.deviceId]: curr.value
    }),
    {}
  );
  const bObject = b.reduce(
    (acc, curr) => ({
      ...acc,
      [curr.deviceId]: curr.value
    }),
    {}
  );
  const definedDevices = /* @__PURE__ */ new Set([...a.map((a2) => a2.deviceId), ...b.map((b2) => b2.deviceId)]);
  return Array.from(definedDevices).map((deviceId) => {
    const aVal = aObject[deviceId];
    const bVal = bObject[deviceId];
    if (aVal === void 0 && bVal === void 0)
      return { deviceId, value: void 0 };
    return (0, import_shallowEqual.default)(aVal, bVal) ? { deviceId, value: aVal } : { deviceId, value: null };
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  normalizeResponsiveValue,
  responsiveShallowEqual,
  setResponsiveValue
});
//# sourceMappingURL=responsive.js.map