import { jsx } from "react/jsx-runtime";
function normalize(value) {
  if (typeof value !== "string") return null;
  const trimmed = value.trim();
  return trimmed.length ? trimmed : null;
}
function mergeStreetLine(address) {
  const street = normalize(address.addressLine1);
  const building = normalize(address.buildingNumber);
  const flat = normalize(address.flatNumber);
  if (!street && !building && !flat) return null;
  let line = street ?? "";
  if (building) line = line ? `${line} ${building}` : building;
  if (flat) line = line ? `${line}/${flat}` : flat;
  return line.length ? line : null;
}
function formatAddressJson(address, format) {
  return {
    format,
    companyName: normalize(address.companyName),
    addressLine1: normalize(address.addressLine1),
    addressLine2: normalize(address.addressLine2),
    buildingNumber: normalize(address.buildingNumber),
    flatNumber: normalize(address.flatNumber),
    postalCode: normalize(address.postalCode),
    city: normalize(address.city),
    region: normalize(address.region),
    country: normalize(address.country)
  };
}
function formatAddressLines(address, format) {
  const json = formatAddressJson(address, format);
  const lines = [];
  if (json.companyName) lines.push(json.companyName);
  if (format === "street_first") {
    const streetLine = mergeStreetLine(address);
    if (streetLine) lines.push(streetLine);
    const supplemental = normalize(address.addressLine2);
    if (supplemental) lines.push(supplemental);
    const postalCity = [json.postalCode, json.city].filter(Boolean).join(" ");
    if (postalCity.length) lines.push(postalCity);
    if (json.region) lines.push(json.region);
    if (json.country) lines.push(json.country);
  } else {
    if (json.addressLine1) {
      const baseLine1 = json.addressLine1;
      const appended = mergeStreetLine(address);
      if (!json.buildingNumber && !json.flatNumber) {
        lines.push(baseLine1);
      } else {
        const composite = appended ?? baseLine1;
        lines.push(composite);
      }
    }
    if (json.addressLine2) lines.push(json.addressLine2);
    const postalCity = [json.postalCode, json.city].filter(Boolean).join(" ");
    if (postalCity.length) lines.push(postalCity);
    if (json.region) lines.push(json.region);
    if (json.country) lines.push(json.country);
  }
  return lines;
}
function formatAddressString(address, format, separator = ", ") {
  return formatAddressLines(address, format).filter(Boolean).join(separator);
}
function AddressView({ address, format, className, lineClassName }) {
  const lines = formatAddressLines(address, format);
  if (!lines.length) return null;
  return /* @__PURE__ */ jsx("div", { className, children: lines.map((line, index) => /* @__PURE__ */ jsx("div", { className: lineClassName, children: line }, `${index}-${line}`)) });
}
export {
  AddressView,
  formatAddressJson,
  formatAddressLines,
  formatAddressString
};
//# sourceMappingURL=addressFormat.js.map
