import {
  TaxOverrideTable,
  TaxRateLine,
  useTaxOverrideTable
} from "./chunk-LFATSJPO.js";
import {
  TaxRegionTable,
  useTaxRegionTable
} from "./chunk-E3DLJSYD.js";
import {
  TaxRegionCard
} from "./chunk-ZBJ56XDB.js";
import {
  getCountryProvinceObjectByIso2
} from "./chunk-N2TQXB44.js";
import {
  formatProvider
} from "./chunk-VW56KWO2.js";
import {
  SingleColumnPage
} from "./chunk-H6MYB7GB.js";
import {
  SingleColumnPageSkeleton
} from "./chunk-INNFZYX2.js";
import {
  useTaxRateTableQuery
} from "./chunk-MG4GKUJW.js";
import {
  useTaxRegionTableQuery
} from "./chunk-AYVH6Q5O.js";
import {
  useTaxRates
} from "./chunk-AEMDNTW5.js";
import {
  useTaxRegion,
  useTaxRegions
} from "./chunk-LYD3C73Z.js";

// src/pages/tax-regions/tax-region-detail/tax-region-detail.tsx
import { useState as useState2, Children } from "react";
import { useLoaderData, useParams } from "react-router-dom";

// src/pages/tax-regions/tax-region-detail/components/tax-region-detail-section/tax-region-detail-section.tsx
import { Badge, Container, Tooltip } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { jsx, jsxs } from "react/jsx-runtime";
var TaxRegionDetailSection = ({
  taxRegion
}) => {
  const { t } = useTranslation();
  const defaultRates = taxRegion.tax_rates.filter((r) => r.is_default === true);
  const showBage = defaultRates.length === 0;
  return /* @__PURE__ */ jsxs(
    Container,
    {
      className: "divide-y p-0",
      "data-testid": "tax-region-detail-section-container",
      children: [
        /* @__PURE__ */ jsx(
          TaxRegionCard,
          {
            taxRegion,
            type: "header",
            asLink: false,
            badge: showBage && /* @__PURE__ */ jsx(Tooltip, { content: t("taxRegions.fields.noDefaultRate.tooltip"), children: /* @__PURE__ */ jsx(
              Badge,
              {
                color: "orange",
                size: "2xsmall",
                className: "cursor-default",
                "data-testid": "tax-region-detail-section-no-default-rate-badge",
                children: t("taxRegions.fields.noDefaultRate.label")
              }
            ) })
          }
        ),
        defaultRates.map((rate) => {
          return /* @__PURE__ */ jsx(TaxRateLine, { taxRate: rate }, rate.id);
        })
      ]
    }
  );
};

// src/pages/tax-regions/tax-region-detail/components/tax-region-province-section/tax-region-province-section.tsx
import { Container as Container2, Heading } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { useTranslation as useTranslation2 } from "react-i18next";
import { jsx as jsx2 } from "react/jsx-runtime";
var PAGE_SIZE = 10;
var PREFIX = "p";
var TaxRegionProvinceSection = ({
  taxRegion,
  showSublevelRegions
}) => {
  const { t } = useTranslation2();
  const { searchParams, raw } = useTaxRegionTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX
  });
  const { tax_regions, count, isPending, isError, error } = useTaxRegions(
    {
      ...searchParams,
      parent_id: taxRegion.id
    },
    {
      placeholderData: keepPreviousData
    }
  );
  const { table } = useTaxRegionTable({
    count,
    data: tax_regions,
    pageSize: PAGE_SIZE,
    prefix: PREFIX
  });
  const provinceObject = getCountryProvinceObjectByIso2(taxRegion.country_code);
  if (!provinceObject && !showSublevelRegions && !taxRegion.children.length) {
    return null;
  }
  const type = provinceObject?.type || "sublevel";
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx2(Container2, { className: "divide-y p-0", "data-testid": "tax-region-province-section-container", children: /* @__PURE__ */ jsx2(
    TaxRegionTable,
    {
      variant: "province",
      action: { to: `provinces/create`, label: t("actions.create") },
      table,
      isPending,
      queryObject: raw,
      count,
      "data-testid": "tax-region-province-section-table",
      children: /* @__PURE__ */ jsx2(Heading, { level: "h2", "data-testid": "tax-region-province-section-heading", children: t(`taxRegions.${type}.header`) })
    }
  ) });
};

// src/pages/tax-regions/tax-region-detail/components/tax-region-override-section/tax-region-override-section.tsx
import { Container as Container3, Heading as Heading2 } from "@medusajs/ui";
import { keepPreviousData as keepPreviousData2 } from "@tanstack/react-query";
import { useTranslation as useTranslation3 } from "react-i18next";
import { jsx as jsx3 } from "react/jsx-runtime";
var PAGE_SIZE2 = 10;
var PREFIX2 = "o";
var TaxRegionOverrideSection = ({
  taxRegion
}) => {
  const { t } = useTranslation3();
  const { searchParams, raw } = useTaxRateTableQuery({
    pageSize: PAGE_SIZE2,
    prefix: PREFIX2
  });
  const { tax_rates, count, isPending, isError, error } = useTaxRates(
    {
      ...searchParams,
      tax_region_id: taxRegion.id,
      is_default: false
    },
    {
      placeholderData: keepPreviousData2
    }
  );
  const { table } = useTaxOverrideTable({
    count,
    data: tax_rates,
    pageSize: PAGE_SIZE2,
    prefix: PREFIX2
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx3(Container3, { className: "p-0", "data-testid": "tax-region-override-section-container", children: /* @__PURE__ */ jsx3(
    TaxOverrideTable,
    {
      isPending,
      table,
      count,
      action: {
        label: t("actions.create"),
        to: "overrides/create"
      },
      queryObject: raw,
      prefix: PREFIX2,
      "data-testid": "tax-region-override-section-table",
      children: /* @__PURE__ */ jsx3(Heading2, { level: "h2", "data-testid": "tax-region-override-section-heading", children: t("taxRegions.taxOverrides.header") })
    }
  ) });
};

// src/pages/tax-regions/tax-region-detail/components/tax-region-sublevel-alert/tax-region-sublevel-alert.tsx
import { Alert, Button, Text } from "@medusajs/ui";
import { useState } from "react";
import { useTranslation as useTranslation4 } from "react-i18next";
import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
var TaxRegionSublevelAlert = ({
  taxRegion,
  showSublevelRegions,
  setShowSublevelRegions
}) => {
  const { t } = useTranslation4();
  const [dismissed, setDismissed] = useState(false);
  const provinceObject = getCountryProvinceObjectByIso2(taxRegion.country_code);
  if (provinceObject || showSublevelRegions || dismissed || taxRegion.children.length) {
    return null;
  }
  return /* @__PURE__ */ jsx4(Alert, { dismissible: true, variant: "info", className: "bg-ui-bg-base", children: /* @__PURE__ */ jsxs2("div", { className: "flex flex-col gap-y-3", children: [
    /* @__PURE__ */ jsxs2("div", { className: "flex flex-col", children: [
      /* @__PURE__ */ jsx4(Text, { size: "small", leading: "compact", weight: "plus", asChild: true, children: /* @__PURE__ */ jsx4("h2", { children: t("taxRegions.fields.sublevels.alert.header") }) }),
      /* @__PURE__ */ jsx4(Text, { size: "small", leading: "compact", className: "text-pretty", children: t("taxRegions.fields.sublevels.alert.description") })
    ] }),
    /* @__PURE__ */ jsxs2("div", { className: "flex items-center gap-x-3", children: [
      /* @__PURE__ */ jsx4(
        Button,
        {
          variant: "secondary",
          size: "small",
          onClick: () => setShowSublevelRegions(true),
          children: t("taxRegions.fields.sublevels.alert.action")
        }
      ),
      /* @__PURE__ */ jsx4(
        Button,
        {
          variant: "transparent",
          size: "small",
          onClick: () => setDismissed(true),
          children: t("actions.hide")
        }
      )
    ] })
  ] }) });
};

// src/pages/tax-regions/tax-region-detail/tax-region-provider-section/tax-region-provider-section.tsx
import { useTranslation as useTranslation5 } from "react-i18next";
import { Container as Container4, Heading as Heading3 } from "@medusajs/ui";
import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
function TaxRegionProviderSection({
  taxRegion
}) {
  const { t } = useTranslation5();
  return /* @__PURE__ */ jsxs3(Container4, { className: "divide-y p-0", children: [
    /* @__PURE__ */ jsx5(Heading3, { level: "h2", className: "px-6 py-4", children: t("taxRegions.provider.header") }),
    /* @__PURE__ */ jsx5("div", { className: "px-6 py-4", children: taxRegion.provider_id && /* @__PURE__ */ jsx5("span", { className: "text-ui-fg-subtle", children: formatProvider(taxRegion.provider_id) }) })
  ] });
}

// src/pages/tax-regions/tax-region-detail/tax-region-detail.tsx
import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
var Root = ({ children }) => {
  const { id } = useParams();
  const [showSublevelRegions, setShowSublevelRegions] = useState2(false);
  const initialData = useLoaderData();
  const {
    tax_region: taxRegion,
    isLoading,
    isError,
    error
  } = useTaxRegion(id, void 0, { initialData });
  if (isLoading || !taxRegion) {
    return /* @__PURE__ */ jsx6(SingleColumnPageSkeleton, { sections: 4, showJSON: true });
  }
  if (isError) {
    throw error;
  }
  return Children.count(children) > 0 ? /* @__PURE__ */ jsx6(SingleColumnPage, { data: taxRegion, showJSON: true, showMetadata: true, children }) : /* @__PURE__ */ jsxs4(SingleColumnPage, { data: taxRegion, showJSON: true, showMetadata: true, children: [
    /* @__PURE__ */ jsx6(
      TaxRegionSublevelAlert,
      {
        taxRegion,
        showSublevelRegions,
        setShowSublevelRegions
      }
    ),
    /* @__PURE__ */ jsx6(TaxRegionDetailSection, { taxRegion }),
    /* @__PURE__ */ jsx6(
      TaxRegionProvinceSection,
      {
        taxRegion,
        showSublevelRegions
      }
    ),
    /* @__PURE__ */ jsx6(TaxRegionOverrideSection, { taxRegion }),
    /* @__PURE__ */ jsx6(TaxRegionProviderSection, { taxRegion })
  ] });
};
var TaxRegionDetailPage = Object.assign(Root, {
  SublevelAlert: TaxRegionSublevelAlert,
  DetailSection: TaxRegionDetailSection,
  ProvinceSection: TaxRegionProvinceSection,
  OverrideSection: TaxRegionOverrideSection,
  ProviderSection: TaxRegionProviderSection
});

export {
  TaxRegionDetailPage
};
