"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 localized_resources_map_exports = {};
__export(localized_resources_map_exports, {
  getInitialState: () => getInitialState,
  getLocalizedResourceId: () => getLocalizedResourceId,
  getSerializedState: () => getSerializedState,
  reducer: () => reducer
});
module.exports = __toCommonJS(localized_resources_map_exports);
var import_controls = require("@makeswift/controls");
var import_actions = require("../actions");
function getInitialState(serializedState = {}) {
  return new Map(
    Object.entries((0, import_controls.mapValues)(serializedState, (resources) => new Map(Object.entries(resources))))
  );
}
function getSerializedState(state) {
  return (0, import_controls.mapValues)(
    Object.fromEntries(state.entries()),
    (resources) => Object.fromEntries(resources.entries())
  );
}
function getLocalizedResourceId(state, locale, resourceId) {
  return state.get(locale)?.get(resourceId);
}
function reducer(state = getInitialState(), action) {
  if (!(0, import_actions.isKnownAction)(action))
    return state;
  switch (action.type) {
    case import_actions.ActionTypes.SET_LOCALIZED_RESOURCE_ID: {
      const { resourceId, localizedResourceId, locale } = action.payload;
      if (locale == null) {
        console.error("Attempt to set a localized resource ID for a non-localized page", {
          resourceId,
          localizedResourceId
        });
        return state;
      }
      return new Map(state).set(
        locale,
        (state.get(locale) ?? /* @__PURE__ */ new Map()).set(resourceId, localizedResourceId)
      );
    }
    case import_actions.ActionTypes.UPDATE_API_CLIENT_CACHE: {
      const { localizedResourcesMap } = action.payload;
      return Object.entries(localizedResourcesMap).reduce((state2, [locale, resources]) => {
        const existing = state2.get(locale) ?? /* @__PURE__ */ new Map();
        const updated = Object.entries(resources).reduce((r, [id, localizedId]) => {
          return r.get(id) != null ? r : new Map(r).set(id, localizedId);
        }, existing);
        return updated === existing ? state2 : new Map(state2).set(locale, updated);
      }, state);
    }
    default:
      return state;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getInitialState,
  getLocalizedResourceId,
  getSerializedState,
  reducer
});
//# sourceMappingURL=localized-resources-map.js.map