"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 read_write_documents_exports = {};
__export(read_write_documents_exports, {
  getDocument: () => getDocument,
  getDocuments: () => getDocuments,
  getInitialState: () => getInitialState,
  isElementReference: () => import_read_only_documents.isElementReference,
  reducer: () => reducer
});
module.exports = __toCommonJS(read_write_documents_exports);
var import_immutable = require("immutable");
var ReadOnlyDocuments = __toESM(require("./read-only-documents"));
var import_actions = require("../actions");
var import_read_only_documents = require("./read-only-documents");
function apply(data, operation) {
  let applied = data;
  operation.forEach((component) => {
    if (component.ld != null)
      applied = (0, import_immutable.removeIn)(applied, component.p);
    if (component.od != null)
      applied = (0, import_immutable.removeIn)(applied, component.p);
    if (component.li != null)
      applied = (0, import_immutable.setIn)(applied, component.p, component.li);
    if (component.oi != null)
      applied = (0, import_immutable.setIn)(applied, component.p, component.oi);
  });
  return applied;
}
function getInitialState({
  documents = []
} = {}) {
  return ReadOnlyDocuments.getInitialState({ documents });
}
function getReadOnlyDocumentsStateSlice(state) {
  return state;
}
function getDocument(state, documentKey) {
  return ReadOnlyDocuments.getDocument(getReadOnlyDocumentsStateSlice(state), documentKey);
}
function getDocuments(state) {
  return ReadOnlyDocuments.getDocuments(getReadOnlyDocumentsStateSlice(state));
}
function reducer(state = getInitialState(), action) {
  const nextState = ReadOnlyDocuments.reducer(state, action);
  if (!(0, import_actions.isKnownAction)(action))
    return state;
  switch (action.type) {
    case import_actions.ActionTypes.CHANGE_DOCUMENT: {
      const document = getDocument(nextState, action.payload.documentKey);
      if (document == null)
        return nextState;
      const currentRootElement = ReadOnlyDocuments.getRootElement(document);
      const nextRootElement = apply(currentRootElement, action.payload.operation);
      return currentRootElement === nextRootElement ? nextState : new Map(nextState).set(action.payload.documentKey, {
        ...document,
        rootElement: nextRootElement
      });
    }
    default:
      return nextState;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getDocument,
  getDocuments,
  getInitialState,
  isElementReference,
  reducer
});
//# sourceMappingURL=read-write-documents.js.map