



(function exposeHestiaPolicyParserCommon(globalScope) {
  "use strict";

  const helpers = globalScope.PolicyPdfParserHelpers;
  if (!helpers) throw new Error("Najpierw wczytaj wspólny silnik importu polis PDF.");

  const { normalizeLine, normalizeForDetection } = helpers;

  function findCommunicationRiskSummary(allText) {
    const allLines = String(allText ?? "").split(/\r?\n/).map(normalizeLine).filter(Boolean);
    const normalizedLines = allLines.map(normalizeForDetection);
    const startIndex = normalizedLines.findIndex((line) => /^PRODUKT UBEZPIECZENIOWY\b/.test(line));
    const endIndex = normalizedLines.findIndex((line, index) => index > startIndex && /^RAZEM DO ZAPLATY\b/.test(line));
    if (startIndex < 0 || endIndex <= startIndex) return helpers.findRiskSummary(allText);
    const lines = allLines.slice(startIndex, endIndex);
    const normalized = normalizedLines.slice(startIndex, endIndex);
    const risks = [];
    const addRisk = (label, amount = "") => {
      if (risks.some((risk) => risk.label === label)) return;
      risks.push({ label, amount: normalizeLine(amount) });
    };
    const pureAmountAfter = (index, maxOffset = 4) => {
      for (let offset = 1; offset <= maxOffset && index + offset < lines.length; offset += 1) {
        if (/^[0-9][0-9\s ]*$/.test(lines[index + offset])) return lines[index + offset];
        if (/^(?:SZYBY|TELEFONICZNY|AUTOCASCO|UBEZPIECZENIA DODATKOWE|CAR ASSISTANCE|NASTEPSTWA)\b/.test(normalized[index + offset])) break;
      }
      return "";
    };
    const lastToken = (line) => line.match(/([0-9]+)\s*$/)?.[1] || "";

    for (let index = 0; index < lines.length; index += 1) {
      const line = normalized[index];
      if (/OC POSIADACZA POJAZDU ZGODNIE Z USTAWA|ZGODNIE Z USTAWA/.test(line)) {
        addRisk("OC", lines[index].match(/zgodnie z Ustawą\s+([0-9][0-9\s ]*)$/i)?.[1]);
      } else if (/^HOLOWANIE POJAZDU\b/.test(line)) {
        addRisk("Holowanie pojazdu", pureAmountAfter(index, 2));
      } else if (/^SZYBY\b/.test(line)) {
        addRisk("Szyby");
      } else if (/^TELEFONICZNY ASYSTENT PRAWNY\b/.test(line)) {
        addRisk("Ochrona prawna", lastToken(lines[index]));
      } else if (/^AUTOCASCO\b/.test(line)) {
        addRisk("AC", pureAmountAfter(index, 4));
      } else if (/CAR ASSISTANCE\s*-\s*WYPADEK/.test(line)) {
        const amount = /\s[0-9]+\s*$/.test(lines[index]) && !/\s000\s*$/.test(lines[index])
          ? lastToken(lines[index])
          : pureAmountAfter(index, 1);
        addRisk("Assistance – wypadek", amount);
      } else if (/CAR ASSISTANCE\s*-\s*AWARIA/.test(line)) {
        const amount = /\s[0-9]+\s*$/.test(lines[index]) && !/\s000\s*$/.test(lines[index])
          ? lastToken(lines[index])
          : pureAmountAfter(index, 1);
        addRisk("Assistance – awaria", amount);
      } else if (/^NASTEPSTWA NIESZCZESLIWYCH WYPADKOW/.test(line)) {
        const combined = `${lines[index]} ${lines[index + 1] || ""}`;
        addRisk("Następstwa nieszczęśliwych wypadków", combined.match(/osoba\s+([0-9]+)\s*$/i)?.[1]);
      }
    }
    return risks
      .map(({ label, amount }) => (amount ? `${label} ${amount} zł` : label))
      .join(" • ")
      .slice(0, 400);
  }

  globalScope.HestiaPolicyParserCommon = Object.freeze({ findCommunicationRiskSummary });
})(globalThis);
