import "./Address-DaePmCRZ.js";
import { m as D, aX as h, u as T, i as g, b as C, j as O } from "./StoreProvider-CBB99upB.js";
import { jsx as f } from "../../../../../preact@10.29.0/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useRef as S, useEffect as b, useCallback as x, useMemo as F, useState as L } from "../../../../../preact@10.29.0/node_modules/preact/hooks/dist/hooks.module.js";
import { useQuery as w } from "../../../../../@tanstack_preact-query@5.95.2_preact@10.29.0/node_modules/@tanstack/preact-query/build/modern/useQuery.js";
import { F as I } from "./Field-CPoHG0G-.js";
import { S as v } from "./Select-seF9rM5K.js";
import { a as N, I as R } from "./InputText-C55stM8O.js";
import { c as E } from "./types-CcxS0801.js";
try {
  let e = typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : {}, r = new e.Error().stack;
  r && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[r] = "a4eb88fd-3e81-4910-95b5-ce2241ab241f", e._sentryDebugIdIdentifier = "sentry-dbid-a4eb88fd-3e81-4910-95b5-ce2241ab241f");
} catch {
}
const X = ({
  currency: e,
  financialReportingDate: r,
  numberOfEmployees: a,
  balanceSheetTotal: t,
  annualTurnover: o
}, n) => ({
  currencyOfFinancialData: e,
  // TODO type this better
  dateOfFinancialData: r,
  employeeCount: typeof a == "number" ? a.toFixed(0) : a,
  balanceSheetTotal: t?.toFixed(2) ?? void 0,
  annualTurnover: o?.toFixed(2) ?? void 0
}), Y = (e, r = !0) => {
  if (e.length === 0)
    throw new Error("No financial reports");
  const a = e.reduce((o, n) => n?.dateOfFinancialData && o?.dateOfFinancialData && n?.dateOfFinancialData > o?.dateOfFinancialData ? n : o, e[0]), t = {
    numberOfEmployees: a.employeeCount ? parseInt(a.employeeCount) : void 0,
    financialReportingDate: a.dateOfFinancialData,
    currency: a.currencyOfFinancialData
  };
  return a.balanceSheetTotal !== void 0 && (t.reportedValueOption = "balanceSheetTotal", t.balanceSheetTotal = parseFloat(a.balanceSheetTotal)), a.annualTurnover !== void 0 && (t.reportedValueOption = "annualTurnover", t.annualTurnover = parseFloat(a.annualTurnover)), r ? t : h("reportedValueOption").from(t);
}, V = async (e, r) => O({
  baseUrl: r,
  path: `datasets/legalForms/${e}`
}), _ = (e, r) => {
  const { baseUrl: a } = C();
  return w({
    queryKey: ["legalForms", e],
    queryFn: () => V(e, a.value),
    ...r
  });
};
function H({
  name: e = "legalForm",
  data: r,
  valid: a,
  errorMessage: t,
  label: o,
  readonly: n,
  handleChangeFor: l,
  countryCode: m
}) {
  const { t: d } = T("common"), { data: s } = _(m), u = S(l);
  b(() => {
    u.current = l;
  }, [l]);
  const y = x(
    (c) => {
      const i = {
        target: {
          name: "legalForm",
          value: c.target.value
        }
      };
      l("legalForm")(i);
    },
    [l]
  );
  return b(() => {
    const i = {
      target: {
        name: "legalFormLabel",
        value: s?.find(
          (p) => p.id === r.legalForm
        )?.name
      }
    };
    u.current("legalFormLabel")(i);
  }, [r.legalForm, s]), /* @__PURE__ */ f(
    I,
    {
      name: e,
      label: o || d((c) => c.businessStructure),
      errorMessage: t,
      isValid: a.legalForm,
      children: (c) => /* @__PURE__ */ f(
        v,
        {
          ...c,
          name: e,
          placeholder: d((i) => i.selectOne),
          selected: r.legalForm,
          items: s ?? [],
          readonly: n && !!r,
          onChange: y
        }
      )
    }
  );
}
const U = D(), q = (e) => e.formatToParts(0.1).find((r) => r.type === "decimal").value, A = (e) => e.formatToParts(0).find((r) => r.type === "currency").value, B = (e, r) => {
  const a = e.lastIndexOf(r), t = Array.from(e).reduce((o, n, l) => /[0-9]/.test(n) ? o + n : l === a ? `${o}.` : o, "");
  return parseFloat(t);
}, J = ({
  locale: e,
  currency: r,
  amount: a,
  onAmountChanged: t,
  ...o
}) => {
  const n = F(
    () => new Intl.NumberFormat(e, { style: "currency", currency: r }),
    [e, r]
  ), l = F(() => q(n), [n]), m = F(() => A(n), [n]), [d, s] = L(""), u = (c) => {
    s(c.currentTarget.value);
  }, y = (c) => {
    const i = c.currentTarget.value, p = B(i, l);
    if (Number.isNaN(p)) {
      U.warn(`Unable to get number from formatted currency: "${i}"`), s(g(a) ? m : n.format(a));
      return;
    }
    t(p);
  };
  return b(() => {
    s(g(a) ? m : n.format(a));
  }, [a, m, n]), /* @__PURE__ */ f(R, { ...o, value: d, onInput: u, onBlur: y });
};
function W(e) {
  return /* @__PURE__ */ f(N, { ...e, type: "number" });
}
const Z = ["currency"];
function ee({
  data: e,
  valid: r,
  errors: a,
  labels: t,
  readonly: o,
  handleChangeFor: n,
  country: l
}) {
  const { t: m } = T("common"), d = E[l], s = d ? d.map((u) => ({ id: u, name: u })) : [];
  return /* @__PURE__ */ f(
    I,
    {
      name: "currency",
      label: t.currency,
      errorMessage: a.currency,
      isValid: r.currency,
      dataTestId: "currency",
      children: (u) => /* @__PURE__ */ f(
        v,
        {
          ...u,
          onChange: n("currency", "blur"),
          name: "currency",
          placeholder: m((y) => y.currencyPlaceholder),
          selected: e.currency,
          items: s,
          readonly: o && !!e.currency
        }
      )
    }
  );
}
export {
  ee as C,
  W as I,
  H as L,
  X as a,
  Z as b,
  J as c,
  Y as m
};
