"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 site_version_exports = {};
__export(site_version_exports, {
  ApiHandlerHeaders: () => ApiHandlerHeaders,
  deserializeSiteVersion: () => deserializeSiteVersion,
  secondsUntilSiteVersionExpiration: () => secondsUntilSiteVersionExpiration,
  serializeSiteVersion: () => serializeSiteVersion,
  siteVersionSchema: () => siteVersionSchema
});
module.exports = __toCommonJS(site_version_exports);
var import_zod = require("zod");
var import_jwt_decode = require("jwt-decode");
const ApiHandlerHeaders = {
  SiteVersion: "X-Makeswift-Site-Version"
};
const decodedTokenPayloadSchema = import_zod.z.object({
  exp: import_zod.z.number()
});
const siteVersionSchema = import_zod.z.object({
  version: import_zod.z.string(),
  token: import_zod.z.string()
});
function millisecondsToSeconds(ms) {
  return Math.floor(ms / 1e3);
}
function serializeSiteVersion(data) {
  const jsonString = JSON.stringify({ version: data.version, token: data.token });
  return jsonString;
}
function parseSerializedSiteVersion(input) {
  try {
    return siteVersionSchema.parse(JSON.parse(input));
  } catch {
    console.error("Failed to parse serialized site version:", input);
    return null;
  }
}
function getSiteVersionTokenPayload(siteVersion) {
  try {
    const decoded = (0, import_jwt_decode.jwtDecode)(siteVersion.token);
    return decodedTokenPayloadSchema.parse(decoded);
  } catch (error) {
    console.error("Invalid site version token");
    throw error;
  }
}
function secondsUntilTokenExpiration(tokenPayload) {
  const currentTime = millisecondsToSeconds(Date.now());
  return Math.max(tokenPayload.exp - currentTime, 0);
}
function secondsUntilSiteVersionExpiration(siteVersion) {
  try {
    const tokenPayload = getSiteVersionTokenPayload(siteVersion);
    return secondsUntilTokenExpiration(tokenPayload);
  } catch (error) {
    console.error("Failed to check site version expiration:", error);
    return 0;
  }
}
function isSiteVersionExpired(siteVersion) {
  return secondsUntilSiteVersionExpiration(siteVersion) <= 0;
}
function deserializeSiteVersion(input) {
  try {
    const parsedSiteVersion = parseSerializedSiteVersion(input);
    if (parsedSiteVersion == null)
      return null;
    if (isSiteVersionExpired(parsedSiteVersion)) {
      console.error("Site version token is expired");
      return null;
    }
    return parsedSiteVersion;
  } catch {
    console.error("Failed to deserialize site version:", input);
    return null;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ApiHandlerHeaders,
  deserializeSiteVersion,
  secondsUntilSiteVersionExpiration,
  serializeSiteVersion,
  siteVersionSchema
});
//# sourceMappingURL=index.js.map