"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 normalizeTypographyUp_exports = {};
__export(normalizeTypographyUp_exports, {
  normalizeTypographyUp: () => normalizeTypographyUp
});
module.exports = __toCommonJS(normalizeTypographyUp_exports);
var import_slate = require("slate");
var import_keys = __toESM(require("../../utils/keys"));
var import_deepEqual = __toESM(require("../../utils/deepEqual"));
var import_normalizeTypographyDown = require("./normalizeTypographyDown");
function shallowAnd(a, b) {
  const bKeys = (0, import_keys.default)(b);
  const aPrime = { ...a };
  const and = {};
  bKeys.forEach((key) => {
    if ((0, import_deepEqual.default)(aPrime[key], b[key]))
      and[key] = aPrime[key];
  });
  return and;
}
function shallowAndTypographies(...typographies) {
  return {
    id: typographies.every((typography) => typography?.id === typographies.at(0)?.id) ? typographies.at(0)?.id : void 0,
    style: [
      ...new Set(
        typographies.flatMap((typography) => typography?.style.map((node) => node.deviceId) ?? [])
      )
    ].flatMap((deviceId) => {
      const stylesForThisDevice = typographies.map((typography) => typography.style.find((s) => s.deviceId === deviceId)?.value).filter(
        (typography) => Boolean(typography)
      );
      if (typographies.length !== stylesForThisDevice.length)
        return [];
      const value = stylesForThisDevice.reduce(
        (acc, curr) => {
          return shallowAnd(acc, curr);
        },
        stylesForThisDevice.at(0)
      );
      if (value == null || Object.keys(value).length === 0) {
        return [];
      }
      return [
        {
          deviceId,
          value
        }
      ];
    })
  };
}
function shallowInverseAnd(a, b) {
  const bKeys = Array.from(/* @__PURE__ */ new Set([...(0, import_keys.default)(b), ...(0, import_keys.default)(a)]));
  const aPrime = { ...a };
  const bPrime = { ...b };
  const xor = {};
  bKeys.forEach((key) => {
    if (!(0, import_deepEqual.default)(aPrime[key], bPrime[key]))
      xor[key] = aPrime[key];
  });
  return xor;
}
function shallowInverseAndTypographies(a, b) {
  const typography = { style: [] };
  if (a?.id !== b.id) {
    typography.id = a?.id;
  }
  if (a && a.style.length > 0) {
    const styles = [];
    for (const style of a.style) {
      const bDeviceTypography = b.style.find((s) => s.deviceId === style.deviceId);
      if (bDeviceTypography == null) {
        styles.push(style);
      } else {
        const uniquePropertiesOfA = shallowInverseAnd(style.value, bDeviceTypography.value);
        if (Object.keys(uniquePropertiesOfA).length > 0) {
          styles.push({
            deviceId: style.deviceId,
            value: uniquePropertiesOfA
          });
        }
      }
    }
    typography.style = styles;
  }
  return typography;
}
function isException(node) {
  return import_slate.Text.isText(node) && node.text === "";
}
function normalizeTypographyUp(editor, entry) {
  const [node, path] = entry;
  if (import_slate.Element.isElement(node) && node.children.length) {
    const typographiesOfChildren = node.children.filter((child) => !isException(child)).map((child) => child.typography);
    const definedTypographiesOfChildren = typographiesOfChildren.filter(
      (typography) => Boolean(typography)
    );
    if (typographiesOfChildren.length === definedTypographiesOfChildren.length) {
      let sharedTypography = shallowAndTypographies(
        ...definedTypographiesOfChildren
      );
      if (sharedTypography.style.length > 0 || sharedTypography.id != null) {
        const rootTypography = (0, import_normalizeTypographyDown.shallowMergeTypographies)(
          sharedTypography,
          node.typography
        );
        import_slate.Editor.withoutNormalizing(editor, () => {
          import_slate.Transforms.setNodes(editor, { typography: rootTypography }, { at: path });
          for (let i = 0; i < node.children.length; i++) {
            const typography = shallowInverseAndTypographies(
              node.children.at(i)?.typography ?? { style: [] },
              sharedTypography
            );
            import_slate.Transforms.setNodes(editor, { typography }, { at: [...path, i] });
          }
        });
        return true;
      }
    }
  }
  return false;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  normalizeTypographyUp
});
//# sourceMappingURL=normalizeTypographyUp.js.map