import { mapValues } from "@makeswift/controls";
import { ActionTypes, isKnownAction } from "../actions";
function getInitialState(serializedState = {}) {
  return new Map(
    Object.entries(mapValues(serializedState, (resources) => new Map(Object.entries(resources))))
  );
}
function getSerializedState(state) {
  return 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 (!isKnownAction(action))
    return state;
  switch (action.type) {
    case 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 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;
  }
}
export {
  getInitialState,
  getLocalizedResourceId,
  getSerializedState,
  reducer
};
//# sourceMappingURL=localized-resources-map.js.map