"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var box_models_exports = {};
__export(box_models_exports, {
  createBox: () => createBox,
  getBox: () => getBox,
  getBoxModel: () => getBoxModel,
  getBoxModels: () => getBoxModels,
  getInitialState: () => getInitialState,
  getMeasurables: () => getMeasurables,
  isMeasurable: () => isMeasurable,
  measure: () => measure,
  parse: () => parse,
  reducer: () => reducer
});
module.exports = __toCommonJS(box_models_exports);
var import_css_box_model = require("css-box-model");
var import_actions = require("../actions");
function parse(rawString) {
  const value = Number(rawString.replace(/px$/, ""));
  return Number.isFinite(value) ? value : 0;
}
function createBox(boxArgs) {
  return (0, import_css_box_model.withScroll)((0, import_css_box_model.createBox)(boxArgs));
}
function getBox(element) {
  return (0, import_css_box_model.withScroll)((0, import_css_box_model.getBox)(element));
}
function isMeasurable(value) {
  if (value instanceof Element)
    return true;
  if (typeof value === "object" && value !== null && "getBoxModel" in value && typeof value.getBoxModel === "function") {
    return true;
  }
  return false;
}
function measure(measurable) {
  if (measurable instanceof Element)
    return getBox(measurable);
  return measurable.getBoxModel();
}
function getInitialState() {
  return { measurables: /* @__PURE__ */ new Map(), boxModels: /* @__PURE__ */ new Map() };
}
function getMeasurables(state) {
  return state.measurables;
}
function getBoxModels(state) {
  return state.boxModels;
}
function getBoxModel(state, documentKey, elementKey) {
  return getBoxModels(state).get(documentKey)?.get(elementKey) ?? null;
}
function reducer(state = getInitialState(), action) {
  if (!(0, import_actions.isKnownAction)(action))
    return state;
  switch (action.type) {
    case import_actions.ActionTypes.REGISTER_MEASURABLE: {
      const { documentKey, elementKey, measurable } = action.payload;
      return {
        ...state,
        measurables: new Map(state.measurables).set(
          documentKey,
          new Map(state.measurables.get(documentKey) ?? []).set(elementKey, measurable)
        )
      };
    }
    case import_actions.ActionTypes.UNREGISTER_MEASURABLE: {
      const { documentKey, elementKey } = action.payload;
      const nextMeasurables = new Map(state.measurables.get(documentKey) ?? []);
      const deleted = nextMeasurables.delete(elementKey);
      return deleted ? { ...state, measurables: new Map(state.measurables).set(documentKey, nextMeasurables) } : state;
    }
    case import_actions.ActionTypes.CHANGE_ELEMENT_BOX_MODELS: {
      const { changedElementBoxModels } = action.payload;
      if (changedElementBoxModels.size === 0)
        return state;
      const nextBoxModels = new Map(state.boxModels);
      changedElementBoxModels.forEach((changedBoxModels, documentKey) => {
        const nextDocumentBoxModels = new Map(nextBoxModels.get(documentKey) ?? []);
        changedBoxModels.forEach((changedBoxModel, elementKey) => {
          if (changedBoxModel == null)
            nextDocumentBoxModels.delete(elementKey);
          else
            nextDocumentBoxModels.set(elementKey, changedBoxModel);
        });
        if (nextDocumentBoxModels.size > 0)
          nextBoxModels.set(documentKey, nextDocumentBoxModels);
        else
          nextBoxModels.delete(documentKey);
      });
      return { ...state, boxModels: nextBoxModels };
    }
    default:
      return state;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createBox,
  getBox,
  getBoxModel,
  getBoxModels,
  getInitialState,
  getMeasurables,
  isMeasurable,
  measure,
  parse,
  reducer
});
//# sourceMappingURL=box-models.js.map