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

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

  const {
    normalizeForDetection,
    firstMatch,
    toIsoDate,
    normalizeVehicleIdentifier,
    splitVehicleMakeAndModel,
    findVehicleExtraDetails,
    cleanPremium,
  } = helpers;

  
  
  
  function buildRiskSummary(allText) {
    const detectionText = normalizeForDetection(allText);
    const risks = [];
    if (/ODPOWIEDZIALNOSC CYWILNA DO WYSOKOSCI SUMY GWARANCYJNEJ/.test(detectionText)) risks.push("OC");
    if (/\bAUTOCASCO\b|\bAC\b.*WARIANT/.test(detectionText)) risks.push("AC");
    if (/ZIELONA KARTA/.test(detectionText)) risks.push("Zielona Karta");
    const assistanceVariant = firstMatch(allText, [/Assistance\s+(Midi|Mini|Maxi|Standard|Premium)/i]);
    risks.push(assistanceVariant ? `Assistance ${assistanceVariant}` : "");
    if (/\bNNW\b/.test(detectionText)) risks.push("NNW");
    if (/UBEZPIECZENIE SZYB/.test(detectionText)) risks.push("Szyby");
    return risks.filter(Boolean).join(" • ").slice(0, 400);
  }

  function findHolderSectionText(allText) {
    const match = allText.match(/Dane ubezpieczającego([\s\S]{0,600}?)(?=Dane (?:właściciela|ubezpieczonego)|UNIQA Towarzystwo)/i);
    return match ? match[1] : "";
  }

  function parseUniqaCommunicationPolicy({ allText }) {
    const holderText = findHolderSectionText(allText) || allText;
    const periodMatch = allText.match(
      /OKRES OCHRONY\s*\n\s*(\d{2}\.\d{2}\.\d{4})(?:\s+\d{1,2}:\d{2})?\s*-\s*\n?\s*(\d{2}\.\d{2}\.\d{4})/i,
    );
    const makeAndModel = splitVehicleMakeAndModel(firstMatch(allText, [
      /Marka samochodu\s+([^\n]{2,80})/i,
    ]));
    const policy = {
      clientName: firstMatch(holderText, [/Imię i nazwisko\s*\/\s*Nazwa\s+([^\n]{3,120})/i]),
      identityNumber: firstMatch(holderText, [/(?:PESEL|NIP|REGON)\s*:?\s*([0-9]{9,11})/i]),
      contact: "",
      policyNumber: firstMatch(allText, [/NUMER POLISY\s*\n\s*([0-9]{4}-[0-9]{6,12})/i]),
      registration: firstMatch(allText, [/Numer rejestracyjny pojazdu\s+([A-Z0-9]{4,10})/i])
        .toLocaleUpperCase("pl"),
      ...makeAndModel,
      vin: normalizeVehicleIdentifier(firstMatch(allText, [
        /Numer VIN\s+([A-Z0-9]{11,17})/i,
      ])),
      firstRegistrationDate: "",
      ...findVehicleExtraDetails(allText),
      clientAddress: firstMatch(holderText, [/Adres\s+([^\n]+(?:\n[^\n]+)?)/i])
        .replace(/\n/g, " "),
      validFrom: periodMatch ? toIsoDate(periodMatch[1]) : "",
      validUntil: periodMatch ? toIsoDate(periodMatch[2]) : "",
      premium: cleanPremium(firstMatch(allText, [
        /SKŁADKA ŁĄCZNA\s*\n\s*([0-9][0-9 .,]*)\s*zł/i,
        /Składka łączna\s*:?\s*(?:w tym[^\n]*\n)?\s*([0-9][0-9 .,]*)\s*zł/i,
      ])),
      riskSummary: buildRiskSummary(allText),
    };
    const missingRequiredFields = ["clientName", "policyNumber", "registration", "validUntil"]
      .filter((fieldName) => !policy[fieldName]);
    if (missingRequiredFields.length > 0) {
      const error = new Error("Nie udało się pewnie odczytać danych klienta, numeru polisy, rejestracji lub okresu z polisy Uniqa.");
      error.code = "POLICY_FIELDS_MISSING";
      error.insurer = "Uniqa";
      error.missingFields = missingRequiredFields;
      throw error;
    }
    return {
      policy,
      missingOptionalFields: ["identityNumber", "validFrom", "premium"]
        .filter((fieldName) => !policy[fieldName]),
    };
  }

  registry.register({
    id: "uniqa-komunikacja",
    insurer: "Uniqa",
    product: "Komunikacja",
    detect: ({ detectionText }) => /UNIQA TOWARZYSTWO UBEZPIECZEN/.test(detectionText)
      && /NUMER POLISY/.test(detectionText)
      && /NUMER REJESTRACYJNY POJAZDU/.test(detectionText),
    parse: parseUniqaCommunicationPolicy,
  });
})(globalThis);
