import { ActionTypes, isKnownAction } from "../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 (!isKnownAction(action))
    return state;
  switch (action.type) {
    case 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 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 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 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;
  }
}
export {
  getInitialState,
  getPropController,
  getPropControllers,
  getPropControllersHandle,
  isPropControllersHandle,
  reducer
};
//# sourceMappingURL=prop-controller-handles.js.map