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

  
  
  
  function getPolicyHolderSection(lines) {
    const startIndex = lines.findIndex((line) => (
      /^UBEZPIECZAJACY(?:\s*\/\s*UBEZPIECZONY)?$/.test(normalizeForDetection(line))
    ));
    if (startIndex < 0) return [];
    const endIndex = lines.findIndex((line, index) => (
      index > startIndex && /^PRODUKT UBEZPIECZENIOWY\b/.test(normalizeForDetection(line))
    ));
    return lines.slice(startIndex, endIndex > startIndex ? endIndex : startIndex + 5);
  }

  function findContact(holderText) {
    const email = firstMatch(holderText, [/([^\s\n,;]+@[^\s\n,;]+)/i]);
    const phone = firstMatch(holderText, [/TEL\.?\s*:?\s*([+]?[0-9][0-9 ()-]{6,20})/i])
      .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+od\s+(\d{4}-\d{2}-\d{2})(?:\s+\d{1,2}:\d{2})?\s+do\s+(\d{4}-\d{2}-\d{2})(?:\s+\d{1,2}:\d{2})?/i,
    );
    return match ? [match[1], match[2]] : ["", ""];
  }

  function buildRiskSummary(allText) {
    const generic = helpers.findRiskSummary(allText);
    
    
    const sum = firstMatch(allText, [
      /(?:OC w życiu prywatnym|Odpowiedzialność cywilna w[\s\n]+życiu prywatnym)[^\n]{0,120}?(?<!\d)(?<!\d[\s\u00a0])(\d{1,3}(?:[\s\u00a0]\d{3})+)\s+\d/iu,
    ]).replace(/\s+/g, " ");
    const base = generic || "Majątek prywatny";
    if (!sum || /suma/i.test(base)) return base;
    return `${base} (suma ${sum} zł)`;
  }

  function parseHestiaPrivatePropertyPolicy({ lines, allText, detectionText }) {
    const holderText = getPolicyHolderSection(lines).join("\n");
    const insurancePeriod = findInsurancePeriod(allText);
    const policy = {
      clientName: firstMatch(holderText || allText, [
        /Ubezpieczający(?:\s*\/\s*Ubezpieczony)?\s*\n([^\n,]{3,180})(?=\s*,\s*DATA URODZENIA\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, [/Polisa\s+(?:E\d{1,2}|MF-O)\s+([0-9A-Z/-]{6,})/i]),
      registration: /MIEJSCE UBEZPIECZENIA/.test(detectionText) ? "Mieszkanie" : "Inne",
      clientAddress: helpers.findClientAddress(holderText),
      validFrom: insurancePeriod[0],
      validUntil: insurancePeriod[1],
      premium: cleanPremium(firstMatch(allText, [
        /RAZEM DO ZAPŁATY\s+([0-9][0-9 .,' ]*)/i,
      ])),
      riskSummary: buildRiskSummary(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 majątkowej polisy E1/E7 ERGO Hestii.");
      error.code = "POLICY_FIELDS_MISSING";
      error.insurer = "Hestia";
      error.missingFields = missingRequiredFields;
      throw error;
    }
    return {
      policy,
      missingOptionalFields: ["identityNumber", "validFrom", "premium"]
        .filter((fieldName) => !policy[fieldName]),
    };
  }

  registry.register({
    id: "hestia-majatek",
    insurer: "Hestia",
    product: "Majątek (E1/E7/MF-O)",
    
    
    
    detect: ({ detectionText }) => /POLISA (?:E\d{1,2}|MF-O)\s+[0-9]/.test(detectionText)
      && !/POLISA E\d{1,2}-KOM/.test(detectionText)
      && /ERGO HESTIA/.test(detectionText)
      && /PRODUKT UBEZPIECZENIOWY/.test(detectionText)
      && !(/POLISA E7\s+[0-9]/.test(detectionText) && /MIEJSCE UBEZPIECZENIA/.test(detectionText)),
    parse: parseHestiaPrivatePropertyPolicy,
  });
})(globalThis);
