import {
  ActionTypes,
  isKnownAction
} from "../actions";
import {
  isElementReference
} from "@makeswift/controls";
function createDocumentReference(key) {
  return { key };
}
const EMBEDDED_DOCUMENT_TYPE = "EMBEDDED_DOCUMENT";
function getRootElement(document) {
  return document.rootElement;
}
function createBaseDocument(key, rootElement, locale) {
  return { key, rootElement, locale };
}
function getInitialState({ documents = [] } = {}) {
  return new Map(documents.map((document) => [document.key, document]));
}
function getDocuments(state) {
  return state;
}
function getDocument(state, documentKey) {
  return getDocuments(state).get(documentKey) ?? null;
}
function reducer(state = getInitialState(), action) {
  if (!isKnownAction(action))
    return state;
  switch (action.type) {
    case ActionTypes.REGISTER_DOCUMENT:
      const { documentKey, document } = action.payload;
      return new Map(state).set(documentKey, { ...document, locale: document.locale ?? null });
    case ActionTypes.UNREGISTER_DOCUMENT: {
      const nextState = new Map(state);
      const deleted = nextState.delete(action.payload.documentKey);
      return deleted ? nextState : state;
    }
    default:
      return state;
  }
}
export {
  EMBEDDED_DOCUMENT_TYPE,
  createBaseDocument,
  createDocumentReference,
  getDocument,
  getDocuments,
  getInitialState,
  getRootElement,
  isElementReference,
  reducer
};
//# sourceMappingURL=read-only-documents.js.map