import {
  DISPLAY_OVERRIDE_ITEMS_LIMIT,
  TaxRegionTaxOverrideEditForm
} from "./chunk-654MHAM6.mjs";
import "./chunk-USSWELGR.mjs";
import "./chunk-LR4WE7QF.mjs";
import "./chunk-ATKJ6XLW.mjs";
import "./chunk-QMRGIWOP.mjs";
import "./chunk-UNTEPIJL.mjs";
import "./chunk-GSZPEFBC.mjs";
import "./chunk-I3VB6NM2.mjs";
import "./chunk-K44PQXQY.mjs";
import "./chunk-KEQOAJC2.mjs";
import "./chunk-EWVOEKUL.mjs";
import "./chunk-UC26CCHZ.mjs";
import "./chunk-5O7HGXFV.mjs";
import "./chunk-IR5DHEKS.mjs";
import "./chunk-DRHDNSJA.mjs";
import "./chunk-5LFJGD27.mjs";
import "./chunk-EJWRAUTW.mjs";
import "./chunk-NTEWUH4C.mjs";
import "./chunk-INE2QCSC.mjs";
import "./chunk-4AKNAVPX.mjs";
import "./chunk-V2CZ23VK.mjs";
import "./chunk-IQBAUTU5.mjs";
import "./chunk-ADOCJB6L.mjs";
import "./chunk-X6BAAGCL.mjs";
import "./chunk-P3UUX2T6.mjs";
import "./chunk-LQTHYS2Z.mjs";
import "./chunk-7TWTWTDT.mjs";
import "./chunk-HQKGZADC.mjs";
import "./chunk-EMIHDNB7.mjs";
import "./chunk-DK4WIVY6.mjs";
import "./chunk-EJUI5JDF.mjs";
import "./chunk-RIV7FKGN.mjs";
import "./chunk-TDK3JDOB.mjs";
import "./chunk-FHSC5X62.mjs";
import "./chunk-XR4GEMGR.mjs";
import "./chunk-WRSGHGAT.mjs";
import "./chunk-MOSRJHJ3.mjs";
import "./chunk-OMC5JCQH.mjs";
import "./chunk-3C6WQ7NH.mjs";
import "./chunk-CBSCX7RE.mjs";
import "./chunk-LT4MVCA7.mjs";
import "./chunk-FUIT667Y.mjs";
import "./chunk-UVGVVAR4.mjs";
import "./chunk-AIJV4VQQ.mjs";
import "./chunk-W7625H47.mjs";
import "./chunk-DLZWPHHO.mjs";
import "./chunk-4DZUJIAL.mjs";
import "./chunk-R65S6ZZV.mjs";
import "./chunk-Q6PYDJZC.mjs";
import "./chunk-XRM7PIRS.mjs";
import "./chunk-C76H5USB.mjs";
import "./chunk-3BQAAPDR.mjs";
import "./chunk-PRLQLEEQ.mjs";
import "./chunk-IUCDCPJU.mjs";
import "./chunk-6HTZNHPT.mjs";
import {
  RouteDrawer
} from "./chunk-OV6YUU3D.mjs";
import "./chunk-ULMMKZ3U.mjs";
import "./chunk-MNXC6Q4F.mjs";
import "./chunk-DG7J63J2.mjs";
import "./chunk-LPEUYMRK.mjs";
import "./chunk-OC7BQLYI.mjs";
import "./chunk-S4DMV3ZT.mjs";
import "./chunk-QJ63TWAK.mjs";
import "./chunk-OBQI23QM.mjs";
import "./chunk-OW2UL5FI.mjs";
import "./chunk-5DUV4AET.mjs";
import "./chunk-FAB3VINY.mjs";
import "./chunk-Q7YBE4IP.mjs";
import {
  useTaxRate
} from "./chunk-HI6URQ7H.mjs";
import "./chunk-6CLQKVAU.mjs";
import "./chunk-KI7TOXBR.mjs";
import {
  useProductTypes
} from "./chunk-QBIQEVTU.mjs";
import "./chunk-WAXMT4IY.mjs";
import "./chunk-DYDGGABK.mjs";
import "./chunk-LGNTHZ5Y.mjs";
import "./chunk-CN7JXSGW.mjs";
import "./chunk-AO2USOOE.mjs";
import "./chunk-KWWLGCSY.mjs";
import "./chunk-5AFMB7XQ.mjs";
import "./chunk-5U6AZEO7.mjs";
import "./chunk-A63RZVX6.mjs";
import {
  useShippingOptions
} from "./chunk-HBXV7ENS.mjs";
import "./chunk-ZBMENGLV.mjs";
import "./chunk-LGLPAUIO.mjs";
import "./chunk-CDORR33H.mjs";
import "./chunk-DE44HCDH.mjs";
import "./chunk-MUOBXNL6.mjs";
import "./chunk-BEQ5XTWH.mjs";
import "./chunk-HP2JH45P.mjs";
import "./chunk-VCSSQVPD.mjs";
import "./chunk-SQDIZZDW.mjs";
import {
  useProducts
} from "./chunk-LA2ILUXH.mjs";
import "./chunk-7AXHHXCX.mjs";
import "./chunk-FXYH54JP.mjs";
import "./chunk-774WSTCC.mjs";
import "./chunk-NFEK63OE.mjs";
import "./chunk-QZ7TP4HQ.mjs";

// src/routes/tax-regions/tax-region-tax-override-edit/tax-region-tax-override-edit.tsx
import { Heading } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
import { jsx, jsxs } from "react/jsx-runtime";
var TaxRegionTaxOverrideEdit = () => {
  const { t } = useTranslation();
  const { tax_rate_id } = useParams();
  const { tax_rate, isPending, isError, error } = useTaxRate(tax_rate_id);
  const { initialValues, isPending: isInitializing } = useDefaultRulesValues(tax_rate);
  const ready = !isPending && !!tax_rate && !isInitializing && !!initialValues;
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
    /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
      /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: t("taxRegions.taxOverrides.edit.header") }) }),
      /* @__PURE__ */ jsx(RouteDrawer.Description, { className: "sr-only", children: t("taxRegions.taxOverrides.edit.hint") })
    ] }),
    ready && /* @__PURE__ */ jsx(
      TaxRegionTaxOverrideEditForm,
      {
        taxRate: tax_rate,
        isCombinable: true,
        initialValues
      }
    )
  ] });
};
var useDefaultRulesValues = (taxRate) => {
  const rules = taxRate?.rules || [];
  const idsByReferenceType = {
    ["product" /* PRODUCT */]: [],
    // [TaxRateRuleReferenceType.PRODUCT_COLLECTION]: [],
    // [TaxRateRuleReferenceType.PRODUCT_TAG]: [],
    ["product_type" /* PRODUCT_TYPE */]: [],
    ["shipping_option" /* SHIPPING_OPTION */]: []
    // [TaxRateRuleReferenceType.CUSTOMER_GROUP]: [],
  };
  rules.sort((a, b) => a.created_at.localeCompare(b.created_at)).forEach((rule) => {
    const reference = rule.reference;
    idsByReferenceType[reference]?.push(rule.reference_id);
  });
  const queries = [
    {
      ids: idsByReferenceType["product" /* PRODUCT */],
      hook: useProducts,
      key: "product" /* PRODUCT */,
      getResult: (result) => result.products.map((product) => ({
        label: product.title,
        value: product.id
      }))
    },
    // {
    //   ids: idsByReferenceType[TaxRateRuleReferenceType.PRODUCT_COLLECTION],
    //   hook: useCollections,
    //   key: TaxRateRuleReferenceType.PRODUCT_COLLECTION,
    //   getResult: (result: HttpTypes.AdminCollectionListResponse) =>
    //     result.collections.map((collection) => ({
    //       label: collection.title!,
    //       value: collection.id!,
    //     })),
    // },
    // {
    //   ids: idsByReferenceType[TaxRateRuleReferenceType.PRODUCT_TAG],
    //   hook: useProductTags,
    //   key: TaxRateRuleReferenceType.PRODUCT_TAG,
    //   getResult: (result: any) =>
    //     result.tags.map((tag: any) => ({
    //       label: tag.value,
    //       value: tag.id,
    //     })),
    // },
    {
      ids: idsByReferenceType["product_type" /* PRODUCT_TYPE */],
      hook: useProductTypes,
      key: "product_type" /* PRODUCT_TYPE */,
      getResult: (result) => result.product_types.map((productType) => ({
        label: productType.value,
        value: productType.id
      }))
    },
    {
      ids: idsByReferenceType["shipping_option" /* SHIPPING_OPTION */],
      hook: useShippingOptions,
      key: "shipping_option" /* SHIPPING_OPTION */,
      getResult: (result) => result.shipping_options.map((shippingOption) => ({
        label: shippingOption.name,
        value: shippingOption.id
      }))
    }
    // {
    //   ids: idsByReferenceType[TaxRateRuleReferenceType.CUSTOMER_GROUP],
    //   hook: useCustomerGroups,
    //   key: TaxRateRuleReferenceType.CUSTOMER_GROUP,
    //   getResult: (
    //     result: HttpTypes.PaginatedResponse<{
    //       customer_groups: HttpTypes.AdminCustomerGroup[]
    //     }>
    //   ) =>
    //     result.customer_groups.map((customerGroup) => ({
    //       label: customerGroup.name!,
    //       value: customerGroup.id,
    //     })),
    // },
  ];
  const queryResults = queries.map(({ ids, hook }) => {
    const enabled = ids.length > 0;
    return {
      result: hook(
        {
          /**
           * Limit fetch to 10 resources for display
           */
          id: ids.length > DISPLAY_OVERRIDE_ITEMS_LIMIT ? ids.slice(0, DISPLAY_OVERRIDE_ITEMS_LIMIT) : ids,
          limit: DISPLAY_OVERRIDE_ITEMS_LIMIT
        },
        { enabled }
      ),
      enabled
    };
  });
  if (!taxRate) {
    return { isPending: true };
  }
  const isPending = queryResults.some(
    ({ result, enabled }) => enabled && result.isPending
  );
  if (isPending) {
    return { isPending };
  }
  queryResults.forEach(({ result, enabled }) => {
    if (enabled && result.isError) {
      throw result.error;
    }
  });
  const initialRulesValues = queries.reduce(
    (acc, { key, getResult }, index) => {
      let initialValues = [];
      if (queryResults[index].enabled) {
        const fetchedEntityList = getResult(queryResults[index].result);
        const entityIdMap = new Map(
          fetchedEntityList.map((entity) => [entity.value, entity])
        );
        const initialIds = idsByReferenceType[key];
        initialValues = initialIds.map((id) => ({
          value: id,
          label: entityIdMap.get(id)?.label || ""
        }));
      }
      return {
        ...acc,
        [key]: initialValues
      };
    },
    {}
  );
  return { initialValues: initialRulesValues, isPending: false };
};
export {
  TaxRegionTaxOverrideEdit as Component
};
