import { Editor, Element, Transforms, Text } from "slate";
import keys from "../../utils/keys";
import deepEqual from "../../utils/deepEqual";
import { shallowMergeTypographies } from "./normalizeTypographyDown";
function shallowAnd(a, b) {
  const bKeys = keys(b);
  const aPrime = { ...a };
  const and = {};
  bKeys.forEach((key) => {
    if (deepEqual(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([...keys(b), ...keys(a)]));
  const aPrime = { ...a };
  const bPrime = { ...b };
  const xor = {};
  bKeys.forEach((key) => {
    if (!deepEqual(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 Text.isText(node) && node.text === "";
}
function normalizeTypographyUp(editor, entry) {
  const [node, path] = entry;
  if (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 = shallowMergeTypographies(
          sharedTypography,
          node.typography
        );
        Editor.withoutNormalizing(editor, () => {
          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
            );
            Transforms.setNodes(editor, { typography }, { at: [...path, i] });
          }
        });
        return true;
      }
    }
  }
  return false;
}
export {
  normalizeTypographyUp
};
//# sourceMappingURL=normalizeTypographyUp.js.map