import {
  TaxRateRuleReferenceType
} from "./chunk-Q4UVXS46.js";
import {
  useCustomerGroupTableColumns,
  useProductTagTableColumns
} from "./chunk-N335VHD3.js";
import {
  useProductTypeTableColumns
} from "./chunk-EBZ5VZEO.js";
import {
  useCollectionTableColumns
} from "./chunk-YNGA3GRX.js";
import {
  useCollectionTableQuery,
  useProductTagTableQuery
} from "./chunk-BI2AYXZQ.js";
import {
  useProductTypeTableQuery
} from "./chunk-VOHEQ7B5.js";
import {
  useCustomerGroupTableQuery
} from "./chunk-VKOE5J66.js";
import {
  useCollectionTableFilters,
  useProductTagTableFilters
} from "./chunk-HHD2VUED.js";
import {
  useProductTypeTableFilters
} from "./chunk-2ZFELO2L.js";
import {
  useCustomerGroupTableFilters
} from "./chunk-CMHI2RPL.js";
import {
  useProductTableColumns
} from "./chunk-AAKR2CB4.js";
import {
  useProductTableQuery
} from "./chunk-BEPAXQMU.js";
import {
  useProductTableFilters
} from "./chunk-LFX6R456.js";
import {
  StackedDrawer,
  StackedFocusModal
} from "./chunk-GDWBOQI5.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-7TQNE3E2.js";
import {
  useProductTags
} from "./chunk-RLRWDHME.js";
import {
  useProductTypes
} from "./chunk-L2NJPDUD.js";
import {
  useCustomerGroups
} from "./chunk-II3CLUXA.js";
import {
  useCollections
} from "./chunk-XKEYHWBS.js";
import {
  useProducts
} from "./chunk-YAUGB4LD.js";

// src/pages/settings/tax-regions/_common/components/target-form.tsx
import { Button, Checkbox } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import {
  createColumnHelper
} from "@tanstack/react-table";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useSearchParams } from "react-router-dom";
import { jsx, jsxs } from "react/jsx-runtime";
function initRowSelection(state) {
  return state.reduce((acc, reference) => {
    acc[reference.value] = true;
    return acc;
  }, {});
}
var TargetForm = ({
  referenceType,
  type,
  setState,
  state
}) => {
  const { t } = useTranslation();
  const Component = type === "focus" ? StackedFocusModal : StackedDrawer;
  const [intermediate, setIntermediate] = useState(state);
  const handleSave = () => {
    setState(intermediate);
  };
  return /* @__PURE__ */ jsxs("div", { className: "flex size-full flex-col overflow-hidden", children: [
    /* @__PURE__ */ jsx(Component.Body, { className: "min-h-0 p-0", children: /* @__PURE__ */ jsx(
      Table,
      {
        referenceType,
        initialRowState: initRowSelection(state),
        intermediate,
        setIntermediate
      }
    ) }),
    /* @__PURE__ */ jsxs(Component.Footer, { children: [
      /* @__PURE__ */ jsx(Component.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", type: "button", children: t("actions.cancel") }) }),
      /* @__PURE__ */ jsx(Button, { type: "button", size: "small", onClick: handleSave, children: t("actions.save") })
    ] })
  ] });
};
var Table = ({ referenceType, ...props }) => {
  switch (referenceType) {
    case "customer_group" /* CUSTOMER_GROUP */:
      return /* @__PURE__ */ jsx(CustomerGroupTable, { ...props });
    case "product" /* PRODUCT */:
      return /* @__PURE__ */ jsx(ProductTable, { ...props });
    case "product_collection" /* PRODUCT_COLLECTION */:
      return /* @__PURE__ */ jsx(ProductCollectionTable, { ...props });
    case "product_type" /* PRODUCT_TYPE */:
      return /* @__PURE__ */ jsx(ProductTypeTable, { ...props });
    case "product_tag" /* PRODUCT_TAG */:
      return /* @__PURE__ */ jsx(ProductTagTable, { ...props });
    default:
      return null;
  }
};
var PAGE_SIZE = 50;
var PREFIX_CUSTOMER_GROUP = "cg";
var CustomerGroupTable = ({
  initialRowState,
  intermediate,
  setIntermediate
}) => {
  const [rowSelection, setRowSelection] = useState(initialRowState);
  useCleanupSearchParams();
  const { searchParams, raw } = useCustomerGroupTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX_CUSTOMER_GROUP
  });
  const { customer_groups, count, isLoading, isError, error } = useCustomerGroups(searchParams, {
    placeholderData: keepPreviousData
  });
  const updater = (value) => {
    const state = typeof value === "function" ? value(rowSelection) : value;
    const currentIds = Object.keys(rowSelection);
    const ids = Object.keys(state);
    const newIds = ids.filter((id) => !currentIds.includes(id));
    const removedIds = currentIds.filter((id) => !ids.includes(id));
    const newCustomerGroups = customer_groups?.filter((cg) => newIds.includes(cg.customer_group_id)).flatMap((cg) => ({ value: cg.customer_group_id, label: cg.customer_group.name })) || [];
    const filteredIntermediate = intermediate.filter(
      (cg) => !removedIds.includes(cg.value)
    );
    setIntermediate([...filteredIntermediate, ...newCustomerGroups]);
    setRowSelection(state);
  };
  const filters = useCustomerGroupTableFilters();
  const columns = useGroupColumns();
  const { table } = useDataTable({
    data: customer_groups || [],
    columns,
    count,
    enablePagination: true,
    enableRowSelection: true,
    getRowId: (row) => row.customer_group_id,
    rowSelection: {
      state: rowSelection,
      updater
    },
    pageSize: PAGE_SIZE,
    prefix: PREFIX_CUSTOMER_GROUP
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      isLoading,
      filters,
      layout: "fill",
      pagination: true,
      search: true,
      prefix: PREFIX_CUSTOMER_GROUP,
      queryObject: raw
    }
  );
};
var cgColumnHelper = createColumnHelper();
var useGroupColumns = () => {
  const base = useCustomerGroupTableColumns();
  return useMemo(
    () => [
      cgColumnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...base
    ],
    [base]
  );
};
var PREFIX_PRODUCT = "p";
var ProductTable = ({
  initialRowState,
  intermediate,
  setIntermediate
}) => {
  const { t } = useTranslation();
  const [rowSelection, setRowSelection] = useState(initialRowState);
  useCleanupSearchParams();
  const { searchParams, raw } = useProductTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT
  });
  const { products, count, isLoading, isError, error } = useProducts(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const updater = (value) => {
    const state = typeof value === "function" ? value(rowSelection) : value;
    const currentIds = Object.keys(rowSelection);
    const ids = Object.keys(state);
    const newIds = ids.filter((id) => !currentIds.includes(id));
    const removedIds = currentIds.filter((id) => !ids.includes(id));
    const newProducts = products?.filter((p) => newIds.includes(p.id)).map((p) => ({
      value: p.id,
      label: p.title
    })) || [];
    const filteredIntermediate = intermediate.filter(
      (p) => !removedIds.includes(p.value)
    );
    setIntermediate([...filteredIntermediate, ...newProducts]);
    setRowSelection(state);
  };
  const filters = useProductTableFilters();
  const columns = useProductColumns();
  const { table } = useDataTable({
    data: products || [],
    columns,
    count,
    enablePagination: true,
    enableRowSelection: true,
    getRowId: (row) => row.id,
    rowSelection: {
      state: rowSelection,
      updater
    },
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      isLoading,
      filters,
      orderBy: [
        { key: "title", label: t("fields.title") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      layout: "fill",
      pagination: true,
      search: true,
      prefix: PREFIX_PRODUCT,
      queryObject: raw
    }
  );
};
var pColumnHelper = createColumnHelper();
var useProductColumns = () => {
  const base = useProductTableColumns();
  return useMemo(
    () => [
      pColumnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...base
    ],
    [base]
  );
};
var PREFIX_PRODUCT_COLLECTION = "pc";
var ProductCollectionTable = ({
  initialRowState,
  intermediate,
  setIntermediate
}) => {
  const { t } = useTranslation();
  const [rowSelection, setRowSelection] = useState(initialRowState);
  useCleanupSearchParams();
  const { searchParams, raw } = useCollectionTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT_COLLECTION
  });
  const { product_collections: collections, count, isLoading, isError, error } = useCollections(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const updater = (value) => {
    const state = typeof value === "function" ? value(rowSelection) : value;
    const currentIds = Object.keys(rowSelection);
    const ids = Object.keys(state);
    const newIds = ids.filter((id) => !currentIds.includes(id));
    const removedIds = currentIds.filter((id) => !ids.includes(id));
    const newCollections = collections?.filter((p) => newIds.includes(p.id)).map((p) => ({
      value: p.id,
      label: p.title
    })) || [];
    const filteredIntermediate = intermediate.filter(
      (p) => !removedIds.includes(p.value)
    );
    setIntermediate([...filteredIntermediate, ...newCollections]);
    setRowSelection(state);
  };
  const filters = useCollectionTableFilters();
  const columns = useCollectionColumns();
  const { table } = useDataTable({
    data: collections || [],
    columns,
    count,
    enablePagination: true,
    enableRowSelection: true,
    getRowId: (row) => row.id,
    rowSelection: {
      state: rowSelection,
      updater
    },
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT_COLLECTION
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      isLoading,
      filters,
      orderBy: [
        { key: "title", label: t("fields.title") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      layout: "fill",
      pagination: true,
      search: true,
      prefix: PREFIX_PRODUCT_COLLECTION,
      queryObject: raw
    }
  );
};
var pcColumnHelper = createColumnHelper();
var useCollectionColumns = () => {
  const base = useCollectionTableColumns();
  return useMemo(
    () => [
      pcColumnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...base
    ],
    [base]
  );
};
var PREFIX_PRODUCT_TYPE = "pt";
var ProductTypeTable = ({
  initialRowState,
  intermediate,
  setIntermediate
}) => {
  const { t } = useTranslation();
  const [rowSelection, setRowSelection] = useState(initialRowState);
  useCleanupSearchParams();
  const { searchParams, raw } = useProductTypeTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT_TYPE
  });
  const { product_types, count, isLoading, isError, error } = useProductTypes(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const updater = (value) => {
    const state = typeof value === "function" ? value(rowSelection) : value;
    const currentIds = Object.keys(rowSelection);
    const ids = Object.keys(state);
    const newIds = ids.filter((id) => !currentIds.includes(id));
    const removedIds = currentIds.filter((id) => !ids.includes(id));
    const newTypes = product_types?.filter((p) => newIds.includes(p.id)).map((p) => ({
      value: p.id,
      label: p.value
    })) || [];
    const filteredIntermediate = intermediate.filter(
      (p) => !removedIds.includes(p.value)
    );
    setIntermediate([...filteredIntermediate, ...newTypes]);
    setRowSelection(state);
  };
  const filters = useProductTypeTableFilters();
  const columns = useProductTypeColumns();
  const { table } = useDataTable({
    data: product_types || [],
    columns,
    count,
    enablePagination: true,
    enableRowSelection: true,
    getRowId: (row) => row.id,
    rowSelection: {
      state: rowSelection,
      updater
    },
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT_TYPE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      isLoading,
      filters,
      orderBy: [
        { key: "value", label: t("fields.value") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      layout: "fill",
      pagination: true,
      search: true,
      prefix: PREFIX_PRODUCT_TYPE,
      queryObject: raw
    }
  );
};
var ptColumnHelper = createColumnHelper();
var useProductTypeColumns = () => {
  const base = useProductTypeTableColumns();
  return useMemo(
    () => [
      ptColumnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...base
    ],
    [base]
  );
};
var PREFIX_PRODUCT_TAG = "ptag";
var ProductTagTable = ({
  initialRowState,
  intermediate,
  setIntermediate
}) => {
  const { t } = useTranslation();
  const [rowSelection, setRowSelection] = useState(initialRowState);
  useCleanupSearchParams();
  const { searchParams, raw } = useProductTagTableQuery({
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT_TAG
  });
  const { product_tags, count, isLoading, isError, error } = useProductTags(
    searchParams,
    {
      placeholderData: keepPreviousData
    }
  );
  const updater = (value) => {
    const state = typeof value === "function" ? value(rowSelection) : value;
    const currentIds = Object.keys(rowSelection);
    const ids = Object.keys(state);
    const newIds = ids.filter((id) => !currentIds.includes(id));
    const removedIds = currentIds.filter((id) => !ids.includes(id));
    const newTags = product_tags?.filter((p) => newIds.includes(p.id)).map((p) => ({
      value: p.id,
      label: p.value
    })) || [];
    const filteredIntermediate = intermediate.filter(
      (p) => !removedIds.includes(p.value)
    );
    setIntermediate([...filteredIntermediate, ...newTags]);
    setRowSelection(state);
  };
  const filters = useProductTagTableFilters();
  const columns = useProductTagColumns();
  const { table } = useDataTable({
    data: product_tags || [],
    columns,
    count,
    enablePagination: true,
    enableRowSelection: true,
    getRowId: (row) => row.id,
    rowSelection: {
      state: rowSelection,
      updater
    },
    pageSize: PAGE_SIZE,
    prefix: PREFIX_PRODUCT_TAG
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx(
    _DataTable,
    {
      table,
      columns,
      pageSize: PAGE_SIZE,
      count,
      isLoading,
      filters,
      orderBy: [
        { key: "value", label: t("fields.value") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") }
      ],
      layout: "fill",
      pagination: true,
      search: true,
      prefix: PREFIX_PRODUCT_TAG,
      queryObject: raw
    }
  );
};
var ptagColumnHelper = createColumnHelper();
var useProductTagColumns = () => {
  const base = useProductTagTableColumns();
  return useMemo(
    () => [
      ptagColumnHelper.display({
        id: "select",
        header: ({ table }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: table.getIsSomePageRowsSelected() ? "indeterminate" : table.getIsAllPageRowsSelected(),
              onCheckedChange: (value) => table.toggleAllPageRowsSelected(!!value)
            }
          );
        },
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx(
            Checkbox,
            {
              checked: row.getIsSelected(),
              onCheckedChange: (value) => row.toggleSelected(!!value),
              onClick: (e) => {
                e.stopPropagation();
              }
            }
          );
        }
      }),
      ...base
    ],
    [base]
  );
};
var useCleanupSearchParams = () => {
  const [_, setSearchParams] = useSearchParams();
  useEffect(() => {
    return () => {
      setSearchParams({});
    };
  }, []);
};

// src/pages/settings/tax-regions/_common/components/target-item.tsx
import { XMarkMini } from "@medusajs/icons";
import { IconButton, Text } from "@medusajs/ui";
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var TargetItem = ({ index, label, onRemove }) => {
  return /* @__PURE__ */ jsxs2("div", { className: "bg-ui-bg-field-component shadow-borders-base flex items-center justify-between gap-2 rounded-md px-2 py-0.5", children: [
    /* @__PURE__ */ jsx2(Text, { size: "small", leading: "compact", children: label }),
    /* @__PURE__ */ jsx2(
      IconButton,
      {
        size: "small",
        variant: "transparent",
        type: "button",
        onClick: () => onRemove(index),
        children: /* @__PURE__ */ jsx2(XMarkMini, {})
      }
    )
  ] });
};

// src/pages/settings/tax-regions/_common/schemas.ts
import { z } from "zod";
var TaxRateRuleReferenceSchema = z.object({
  value: z.string(),
  label: z.string()
});
var TaxRateRuleTargetSchema = z.object({
  reference_type: z.nativeEnum(TaxRateRuleReferenceType),
  references: z.array(TaxRateRuleReferenceSchema)
});

// src/pages/settings/tax-regions/_common/utils.ts
var createTaxRulePayload = (target) => {
  return target.references.map((reference) => ({
    reference: target.reference_type,
    reference_id: reference.value
  }));
};

export {
  TargetForm,
  TargetItem,
  TaxRateRuleReferenceSchema,
  createTaxRulePayload
};
