import {
  createBox as createBoxWithoutScroll,
  getBox as getBoxWithoutScroll,
  withScroll
} from "css-box-model";
import { ActionTypes, isKnownAction } from "../actions";
function parse(rawString) {
  const value = Number(rawString.replace(/px$/, ""));
  return Number.isFinite(value) ? value : 0;
}
function createBox(boxArgs) {
  return withScroll(createBoxWithoutScroll(boxArgs));
}
function getBox(element) {
  return withScroll(getBoxWithoutScroll(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 (!isKnownAction(action))
    return state;
  switch (action.type) {
    case 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 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 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;
  }
}
export {
  createBox,
  getBox,
  getBoxModel,
  getBoxModels,
  getInitialState,
  getMeasurables,
  isMeasurable,
  measure,
  parse,
  reducer
};
//# sourceMappingURL=box-models.js.map