"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 normalize_ricos_document_exports = {};
__export(normalize_ricos_document_exports, {
  normalizeRicosDocument: () => normalizeRicosDocument
});
module.exports = __toCommonJS(normalize_ricos_document_exports);
var import_lodash = __toESM(require("lodash"));
var import_bulleted_list_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/bulleted_list.defaults.json.js"));
var import_checkbox_list_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/checkbox_list.defaults.json.js"));
var import_ordered_list_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/ordered_list.defaults.json.js"));
var import_paragraph_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/paragraph.defaults.json.js"));
var import_table_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/table.defaults.json.js"));
var import_modify = require("./RicosContentAPI/modify.js");
const normalizeRicosDocument = (content) => {
  const result = (0, import_modify.modify)(content).set((node) => {
    return import_lodash.default.flow(
      removeEmptyDecorations,
      removeEmptyNodesField,
      removeDefaultsFromDataFields,
      removeUndefinedFields
    )(node);
  });
  return cleanDocumentStyle(removeMetadata(result));
};
const removeUndefinedFields = (node) => {
  const result = Object.entries(node).reduce((acc, [key, value]) => {
    if (value === void 0) {
      return acc;
    }
    acc[key] = value;
    return acc;
  }, {});
  return result;
};
const removeMetadata = (content) => {
  const { metadata: _, ...rest } = content;
  return rest;
};
const cleanDocumentStyle = (content) => {
  const { documentStyle, ...rest } = content;
  if (!documentStyle || !Object.keys(documentStyle).length) {
    return rest;
  }
  const result = { ...rest, documentStyle: removeDefaults(documentStyle, documentStyleDefaults) };
  return result;
};
const removeEmptyDecorations = (node) => {
  if (node.type === "TEXT") {
    if (node.textData?.decorations?.length) {
      return node;
    }
    return { ...node, textData: import_lodash.default.omit(node.textData, "decorations") };
  }
  return node;
};
const removeEmptyNodesField = (node) => {
  return !node.nodes?.length ? import_lodash.default.omit(node, "nodes") : node;
};
const removeDefaultsFromDataFields = (node) => {
  if (node.type === "PARAGRAPH") {
    const { paragraphData, ...rest } = node;
    const normalizedData = removeDefaults(paragraphData ?? {}, import_paragraph_defaults_json.default);
    return { ...rest, ...normalizedData ? { paragraphData: normalizedData } : {} };
  }
  if (node.type === "BULLETED_LIST") {
    const { bulletedListData, ...rest } = node;
    const normalizedData = removeDefaults(bulletedListData ?? {}, import_bulleted_list_defaults_json.default);
    return {
      ...rest,
      // For now we cannot remove the indentation since it is int32 in proto (not optional)
      ...normalizedData ? { bulletedListData: { indentation: 0, ...normalizedData } } : {}
    };
  }
  if (node.type === "TABLE") {
    const { tableData, ...rest } = node;
    const normalizedData = removeDefaults(tableData ?? {}, import_table_defaults_json.default);
    return {
      ...rest,
      ...normalizedData ? { tableData: normalizedData } : {}
    };
  }
  if (node.type === "ORDERED_LIST") {
    const { orderedListData, ...rest } = node;
    const normalizedData = removeDefaults(orderedListData ?? {}, import_ordered_list_defaults_json.default);
    return {
      ...rest,
      // For now we cannot remove the indentation since it is int32 in proto (not optional)
      ...normalizedData ? { orderedListData: { indentation: 0, ...normalizedData } } : {}
    };
  }
  if (node.type === "CHECKBOX_LIST") {
    const { checkboxListData, ...rest } = node;
    const normalizedData = removeDefaults(checkboxListData ?? {}, import_checkbox_list_defaults_json.default);
    return {
      ...rest,
      ...normalizedData ? { checkboxListData: { indentation: 0, ...normalizedData } } : {}
    };
  }
  return node;
};
function removeDefaults(data, defaults) {
  const result = Object.entries(data).reduce((acc, [key, value]) => {
    if (value === void 0) {
      return acc;
    }
    if (import_lodash.default.isEqual(value, defaults[key])) {
      return acc;
    }
    if (import_lodash.default.isPlainObject(value) && import_lodash.default.isPlainObject(defaults[key])) {
      const nested = removeDefaults(value, defaults[key]);
      if (nested !== void 0) {
        acc[key] = nested;
      }
      return acc;
    }
    acc[key] = value;
    return acc;
  }, {});
  return Object.keys(result).length ? result : void 0;
}
const textNodeStyleDefaults = {
  decorations: [],
  nodeStyle: {}
};
const documentStyleDefaults = {
  headerOne: textNodeStyleDefaults,
  headerTwo: textNodeStyleDefaults,
  headerThree: textNodeStyleDefaults,
  headerFour: textNodeStyleDefaults,
  headerFive: textNodeStyleDefaults,
  headerSix: textNodeStyleDefaults,
  paragraph: textNodeStyleDefaults,
  blockquote: textNodeStyleDefaults,
  codeBlock: textNodeStyleDefaults
};
//# sourceMappingURL=normalize-ricos-document.js.map
