import {
  createPortfolioPdfIdentity,
  normalizePortfolioPdfInsurer,
} from "./ldpwrbpjqvuxzylkmjve.mjs";

function expectedInsurer(value) {
  return normalizePortfolioPdfInsurer(value);
}

function identityFromObject(value, insurer) {
  const sourceInsurer = value?.insurer ?? insurer;
  const identity = createPortfolioPdfIdentity({
    insurer: sourceInsurer,
    policyNumber: value?.policyNumber,
    validFrom: value?.validFrom,
    validUntil: value?.validUntil,
  });
  return identity.insurer === insurer ? identity : null;
}

function identityFromString(value, insurer) {
  const raw = String(value ?? "").trim();
  if (!raw) return null;
  const parts = raw.split(":");

  if (parts.length === 4) {
    try {
      const identity = createPortfolioPdfIdentity({
        insurer: parts[0],
        policyNumber: parts[1],
        validFrom: parts[2],
        validUntil: parts[3],
      });
      return identity.insurer === insurer ? identity : null;
    } catch {
      return null;
    }
  }

  if (parts.length === 2) {
    try {
      if (normalizePortfolioPdfInsurer(parts[0]) === insurer) {
        return createPortfolioPdfIdentity({
          insurer,
          policyNumber: parts[1],
        });
      }
    } catch {
      
      
    }
  }

  try {
    return createPortfolioPdfIdentity({
      insurer,
      policyNumber: raw,
    });
  } catch {
    return null;
  }
}

 
export function normalizePortfolioPdfPolicySelector(value, { insurer } = {}) {
  const normalizedInsurer = expectedInsurer(insurer);
  let identity;
  try {
    identity = value && typeof value === "object" && !Array.isArray(value)
      ? identityFromObject(value, normalizedInsurer)
      : identityFromString(value, normalizedInsurer);
  } catch {
    identity = null;
  }
  if (!identity) return null;
  return {
    ...identity,
    hasPeriod: Boolean(identity.validFrom || identity.validUntil),
  };
}

export function createPortfolioPdfPolicySelectorSet(values, { insurer } = {}) {
  const normalizedInsurer = expectedInsurer(insurer);
  const exactKeys = new Set();
  const legacyPolicyNumbers = new Set();
  for (const value of Array.isArray(values) ? values : []) {
    const selector = normalizePortfolioPdfPolicySelector(value, {
      insurer: normalizedInsurer,
    });
    if (!selector) continue;
    if (selector.hasPeriod) exactKeys.add(selector.key);
    else legacyPolicyNumbers.add(selector.policyNumber);
  }
  return Object.freeze({
    insurer: normalizedInsurer,
    exactKeys,
    legacyPolicyNumbers,
  });
}

export function portfolioPdfPolicySelectorMatches(selectorSet, policy) {
  if (
    !selectorSet
    || !(selectorSet.exactKeys instanceof Set)
    || !(selectorSet.legacyPolicyNumbers instanceof Set)
  ) {
    throw new TypeError("Nieprawidłowy zestaw selektorów PDF.");
  }
  const selector = normalizePortfolioPdfPolicySelector(policy, {
    insurer: selectorSet.insurer,
  });
  if (!selector) return false;
  if (selector.hasPeriod) return selectorSet.exactKeys.has(selector.key);
  return selectorSet.legacyPolicyNumbers.has(selector.policyNumber);
}

export function countUnskippedPortfolioPdfSelectors({
  known = [],
  skipped = [],
  insurer,
} = {}) {
  const normalizedInsurer = expectedInsurer(insurer);
  const skippedTokens = new Set();
  for (const value of Array.isArray(skipped) ? skipped : []) {
    const selector = normalizePortfolioPdfPolicySelector(value, {
      insurer: normalizedInsurer,
    });
    if (!selector) continue;
    skippedTokens.add(selector.hasPeriod
      ? `period:${selector.key}`
      : `legacy:${selector.policyNumber}`);
  }
  const pendingTokens = new Set();
  for (const value of Array.isArray(known) ? known : []) {
    const selector = normalizePortfolioPdfPolicySelector(value, {
      insurer: normalizedInsurer,
    });
    if (!selector) continue;
    const token = selector.hasPeriod
      ? `period:${selector.key}`
      : `legacy:${selector.policyNumber}`;
    if (!skippedTokens.has(token)) pendingTokens.add(token);
  }
  return pendingTokens.size;
}

export function createPortfolioPdfTransportIdentity(policy, { insurer } = {}) {
  const selector = normalizePortfolioPdfPolicySelector(policy, { insurer });
  if (!selector) throw new TypeError("Nieprawidłowa tożsamość polisy PDF.");
  return {
    insurer: selector.insurer,
    policyNumber: selector.policyNumber,
    ...(selector.validFrom ? { validFrom: selector.validFrom } : {}),
    ...(selector.validUntil ? { validUntil: selector.validUntil } : {}),
  };
}

export function restorePortfolioPdfTransportPolicy(value, {
  insurer,
  insurerLabel = insurer,
} = {}) {
  const selector = normalizePortfolioPdfPolicySelector(value, { insurer });
  if (!selector) return null;
  return {
    insurer: String(insurerLabel || selector.insurer),
    policyNumber: selector.policyNumber,
    validFrom: selector.validFrom,
    validUntil: selector.validUntil,
  };
}
