import { readJsonFromLocalStorage, writeJsonToLocalStorage, removeLocalStorageKey } from "./safeLocalStorage.js";
function readVersionedPreference(key, version, isValid, fallback, options) {
  const raw = readJsonFromLocalStorage(key, null);
  if (raw && typeof raw === "object" && !Array.isArray(raw) && "v" in raw && "data" in raw) {
    const envelope = raw;
    return envelope.v === version && isValid(envelope.data) ? envelope.data : fallback;
  }
  if (options?.legacyIsValid && options.legacyIsValid(raw)) return raw;
  return fallback;
}
function writeVersionedPreference(key, version, data) {
  writeJsonToLocalStorage(key, { v: version, data });
}
function clearVersionedPreference(key) {
  removeLocalStorageKey(key);
}
function isStringArray(value) {
  return Array.isArray(value) && value.every((item) => typeof item === "string");
}
function readVersionedIdSet(key, version) {
  return new Set(readVersionedPreference(key, version, isStringArray, [], { legacyIsValid: isStringArray }));
}
function writeVersionedIdSet(key, version, ids) {
  writeVersionedPreference(key, version, Array.from(ids));
}
export {
  clearVersionedPreference,
  readVersionedIdSet,
  readVersionedPreference,
  writeVersionedIdSet,
  writeVersionedPreference
};
//# sourceMappingURL=versionedPreference.js.map
