(function registerPzuHomeLegacyPolicyParser(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, cleanPremium } = helpers;

  
  
  
  
  const INLINE_HOLDER_PATTERN = /^\d*\s*UBEZPIECZAJACY\s*:\s*\S/;

  function hasInlineHolderLine(lines) {
    return lines.some((line) => INLINE_HOLDER_PATTERN.test(normalizeForDetection(line)));
  }

  function hasStandaloneHolderLine(lines) {
    return lines.some((line) => /^UBEZPIECZAJACY$/.test(normalizeForDetection(line)));
  }

  function getPolicyHolderSection(lines) {
    const startIndex = lines.findIndex((line) => INLINE_HOLDER_PATTERN.test(normalizeForDetection(line)));
    if (startIndex < 0) return [];
    const endIndex = lines.findIndex((line, index) => (
      index > startIndex
      && /^\d*\s*(?:UBEZPIECZONY\s*:|MIEJSCE UBEZPIECZENIA\s*:)/.test(normalizeForDetection(line))
    ));
    return lines.slice(startIndex, endIndex > startIndex ? endIndex : startIndex + 5);
  }

  function findContact(holderText) {
    const rawEmail = firstMatch(holderText, [/E-?mail\s*:\s*([^\n]+?)(?=\s+Telefon\s*:|$)/i]);
    const rawPhone = firstMatch(holderText, [/Telefon\s*:\s*([^\n]+)/i]);
    const email = /@/.test(rawEmail) && !/NIEUSTALON|ODMOW/i.test(normalizeForDetection(rawEmail))
      ? rawEmail
      : "";
    const phoneDigits = rawPhone.replace(/\D/g, "");
    const phone = phoneDigits.length >= 7 && !/NIEUSTALON|ODMOW/i.test(normalizeForDetection(rawPhone))
      ? rawPhone.replace(/[()\s-]+/g, "")
      : "";
    return [
      phone ? `Telefon: ${phone}` : "",
      email ? `E-mail: ${email}` : "",
    ].filter(Boolean).join("\n");
  }

  function findInsurancePeriod(allText) {
    const match = allText.match(
      /Okres ubezpieczenia\s*:\s*od\s+(\d{2}\.\d{2}\.\d{4})(?:\s*r\.)?(?:\s*godz\.\s*\d{1,2}:\d{2})?\s+do\s+(\d{2}\.\d{2}\.\d{4})(?:\s*r\.)?/i,
    );
    return match ? [toIsoDate(match[1]), toIsoDate(match[2])] : ["", ""];
  }

  function parsePzuHomeLegacyPolicy({ lines, allText }) {
    const holderLines = getPolicyHolderSection(lines);
    const holderText = holderLines.join("\n");
    const insurancePeriod = findInsurancePeriod(allText);
    const policy = {
      clientName: firstMatch(holderText, [/Ubezpieczający\s*:\s*([^\n]+)/i]),
      identityNumber: firstMatch(holderText, [/(?:REGON|PESEL|NIP)\s*:\s*([0-9][0-9 .-]{7,18})/i]).replace(/\D/g, ""),
      contact: findContact(holderText),
      policyNumber: firstMatch(allText, [/Numer polisy\s+([0-9A-Z/-]{6,})/i]),
      registration: "Mieszkanie",
      clientAddress: helpers.findClientAddress(holderText),
      validFrom: insurancePeriod[0],
      validUntil: insurancePeriod[1],
      premium: cleanPremium(firstMatch(allText, [
        /Składka łączna\s*:?\s*([0-9][0-9 .,' ]*)\s*zł/i,
      ])),
      riskSummary: helpers.findRiskSummary(allText),
    };
    const missingRequiredFields = ["clientName", "policyNumber", "validUntil"]
      .filter((fieldName) => !policy[fieldName]);
    if (missingRequiredFields.length > 0) {
      const error = new Error("Nie udało się pewnie odczytać danych klienta, numeru polisy lub okresu z polisy PZU Dom (starszy wzór).");
      error.code = "POLICY_FIELDS_MISSING";
      error.insurer = "PZU";
      error.missingFields = missingRequiredFields;
      throw error;
    }
    return {
      policy,
      missingOptionalFields: ["identityNumber", "validFrom", "premium"]
        .filter((fieldName) => !policy[fieldName]),
    };
  }

  registry.register({
    id: "pzu-dom-2024",
    insurer: "PZU",
    product: "Dom (starszy wzór)",
    detect: ({ detectionText, lines }) => /POLISA UBEZPIECZENIA PZU DOM/.test(detectionText)
      && /POWSZECHNY ZAKLAD UBEZPIECZEN/.test(detectionText)
      && hasInlineHolderLine(lines)
      && !hasStandaloneHolderLine(lines),
    parse: parsePzuHomeLegacyPolicy,
  });
})(globalThis);
