(function registerWienerAutoPolicyParser(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 {
    firstMatch,
    normalizeVehicleIdentifier,
    findVehicleMakeAndModel,
    findFirstRegistrationDate,
    findVehicleExtraDetails,
    findClientAddress,
    cleanPremium,
  } = helpers;

  
  const { toIsoDate } = helpers;

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

  function parseWienerAutoPolicy({ allText }) {
    const insurancePeriod = findInsurancePeriod(allText);
    const policy = {
      clientName: firstMatch(allText, [
        /Imię i nazwisko\s*:\s*([^\n]{3,120})/i,
        /Posiadacz pojazdu\s*\/\s*Vehicle owner\s+([^,\n]{3,120})/i,
      ]),
      identityNumber: firstMatch(allText, [
        /Nr PESEL\s*:\s*(\d{11})/i,
      ]),
      contact: "",
      policyNumber: firstMatch(allText, [
        /Seria i numer\s+([A-Z0-9/-]{6,})/i,
        /Numer polisy\s*:\s*([A-Z0-9/-]{6,})/i,
      ]),
      registration: firstMatch(allText, [
        /Numer rejestracyjny\s*:\s*([A-Z0-9][A-Z0-9 -]{3,11})/i,
        /Nr rejestracyjny\s*\/\s*Registration no\.\s+([A-Z0-9][A-Z0-9 -]{3,11})/i,
      ]).replace(/\s+/g, "").toLocaleUpperCase("pl"),
      ...findVehicleMakeAndModel(allText),
      vin: normalizeVehicleIdentifier(firstMatch(allText, [
        /Nr podwozia\s*\/\s*nadwozia\s*:\s*([A-Z0-9][A-Z0-9 -]{3,23})/i,
        /Nr nadwozia VIN\s*\/\s*Vehicle Identification No\.\s+([A-Z0-9][A-Z0-9 -]{3,23})/i,
      ])),
      firstRegistrationDate: findFirstRegistrationDate(allText),
      ...findVehicleExtraDetails(allText),
      clientAddress: findClientAddress(allText),
      validFrom: insurancePeriod[0],
      validUntil: insurancePeriod[1],
      premium: cleanPremium(firstMatch(allText, [
        /Składka łączna\s*:\s*([0-9][0-9 .,'\u00a0]*)\s*zł/i,
      ])),
      riskSummary: helpers.findRiskSummary(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 Wiener Pakiet AUTO.");
      error.code = "POLICY_FIELDS_MISSING";
      error.insurer = "Wiener";
      error.missingFields = missingRequiredFields;
      throw error;
    }
    return {
      policy,
      missingOptionalFields: ["identityNumber", "validFrom", "premium"]
        .filter((fieldName) => !policy[fieldName]),
    };
  }

  registry.register({
    id: "wiener-auto",
    insurer: "Wiener",
    product: "Pakiet AUTO",
    detect: ({ detectionText }) => /POLISA PAKIET AUTO/.test(detectionText)
      && /WIENER JEST ZNAKIEM TOWAROWYM/.test(detectionText),
    parse: parseWienerAutoPolicy,
  });
})(globalThis);
