"use strict";
require("./index-Br7AzmuA.cjs");
const errorCodes = require("./errorCodes-ifTrQJQk.cjs");
const dayjs = require("dayjs");
function extractHostTimezone(eventType) {
  if (eventType?.userId && eventType?.owner?.timeZone) {
    return eventType.owner.timeZone;
  } else if (eventType?.teamId) {
    if (eventType.hosts && eventType.hosts.length > 0 && eventType.hosts[0]?.user?.timeZone) {
      return eventType.hosts[0].user.timeZone;
    } else if (eventType.team?.members && eventType.team.members.length > 0 && eventType.team.members[0]?.user?.timeZone) {
      return eventType.team.members[0]?.user?.timeZone;
    }
  }
  return dayjs.tz.guess();
}
function hasExpiryTimePassed(expiresAt, timezone) {
  if (!expiresAt)
    return false;
  if (timezone) {
    const now = dayjs().tz(timezone);
    const expiration = dayjs(expiresAt).tz(timezone);
    return expiration.isBefore(now);
  } else {
    const now = dayjs();
    const expiration = dayjs(expiresAt);
    return expiration.isBefore(now);
  }
}
function isTimeBasedExpired(expiresAt, eventType) {
  const hostTimezone = extractHostTimezone(eventType);
  return hasExpiryTimePassed(expiresAt, hostTimezone);
}
function isUsageBasedExpired(usageCount, maxUsageCount) {
  if (!maxUsageCount || maxUsageCount <= 0)
    return false;
  return usageCount >= maxUsageCount;
}
function validateHashedLinkData(linkData) {
  if (isTimeBasedExpired(linkData.expiresAt, linkData.eventType)) {
    throw new Error(errorCodes.ErrorCode.PrivateLinkExpired);
  }
  if (isUsageBasedExpired(linkData.usageCount, linkData.maxUsageCount)) {
    throw new Error(errorCodes.ErrorCode.PrivateLinkExpired);
  }
}
function isLinkExpired(link, timezone) {
  if (link.expiresAt)
    return hasExpiryTimePassed(link.expiresAt, timezone);
  return isUsageBasedExpired(link.usageCount || 0, link.maxUsageCount);
}
exports.isLinkExpired = isLinkExpired;
exports.validateHashedLinkData = validateHashedLinkData;
