import { Editor, Transforms } from "slate";
import { getSelection } from "../selectors";
import shallowEqual from "../../utils/shallowEqual";
function setResponsiveValue(editor, key, value, options) {
  Editor.withoutNormalizing(editor, () => {
    const at = getSelection(editor);
    if (!at)
      return;
    const atRef = Editor.rangeRef(editor, at);
    if (atRef.current) {
      Transforms.setNodes(
        editor,
        {
          slice: true
        },
        {
          at: atRef.current,
          match: options.match,
          split: options.split
        }
      );
    }
    if (atRef.current) {
      const nodesToUpdate = Array.from(
        Editor.nodes(editor, {
          at: atRef.current,
          match: (node, path) => options.match(node, path) && node.slice === true
        })
      );
      for (const [, path] of nodesToUpdate) {
        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) {
      Transforms.unsetNodes(editor, key, { at: path });
      return true;
    }
    if (node?.slice != null) {
      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 shallowEqual(aVal, bVal) ? { deviceId, value: aVal } : { deviceId, value: null };
  });
}
export {
  normalizeResponsiveValue,
  responsiveShallowEqual,
  setResponsiveValue
};
//# sourceMappingURL=responsive.js.map