"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 prop_controller_handles_exports = {};
__export(prop_controller_handles_exports, {
  getInitialState: () => getInitialState,
  getPropController: () => getPropController,
  getPropControllers: () => getPropControllers,
  getPropControllersHandle: () => getPropControllersHandle,
  isPropControllersHandle: () => isPropControllersHandle,
  reducer: () => reducer
});
module.exports = __toCommonJS(prop_controller_handles_exports);
var import_actions = require("../actions");
function isPropControllersHandle(value) {
  if (typeof value === "object" && value !== null && "setPropControllers" in value && typeof value.setPropControllers === "function") {
    return true;
  }
  return false;
}
function getInitialState() {
  return { handles: /* @__PURE__ */ new Map(), instances: /* @__PURE__ */ new Map() };
}
function getPropControllersHandle(state, documentKey, elementKey) {
  return state.handles.get(documentKey)?.get(elementKey) ?? null;
}
function getPropControllers(state, documentKey, elementKey) {
  return state.instances.get(documentKey)?.get(elementKey) ?? null;
}
function getPropController(state, documentKey, elementKey, propName) {
  return getPropControllers(state, documentKey, elementKey)?.[propName] ?? null;
}
function reducer(state = getInitialState(), action) {
  if (!(0, import_actions.isKnownAction)(action))
    return state;
  switch (action.type) {
    case import_actions.ActionTypes.REGISTER_PROP_CONTROLLERS_HANDLE: {
      const { documentKey, elementKey, handle } = action.payload;
      return {
        ...state,
        handles: new Map(state.handles).set(
          documentKey,
          new Map(state.handles.get(documentKey) ?? []).set(elementKey, handle)
        )
      };
    }
    case import_actions.ActionTypes.UNREGISTER_PROP_CONTROLLERS_HANDLE: {
      const { documentKey, elementKey } = action.payload;
      const nextHandles = new Map(state.handles.get(documentKey) ?? []);
      const deleted = nextHandles.delete(elementKey);
      return deleted ? { ...state, handles: new Map(state.handles).set(documentKey, nextHandles) } : state;
    }
    case import_actions.ActionTypes.REGISTER_PROP_CONTROLLERS: {
      const { documentKey, elementKey, propControllers } = action.payload;
      return {
        ...state,
        instances: new Map(state.instances).set(
          documentKey,
          new Map(state.instances.get(documentKey) ?? []).set(elementKey, propControllers)
        )
      };
    }
    case import_actions.ActionTypes.UNREGISTER_PROP_CONTROLLERS: {
      const { documentKey, elementKey } = action.payload;
      const nextInstances = new Map(state.instances.get(documentKey) ?? []);
      const deleted = nextInstances.delete(elementKey);
      return deleted ? { ...state, instances: new Map(state.instances).set(documentKey, nextInstances) } : state;
    }
    default:
      return state;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getInitialState,
  getPropController,
  getPropControllers,
  getPropControllersHandle,
  isPropControllersHandle,
  reducer
});
//# sourceMappingURL=prop-controller-handles.js.map