import { E as compilerRuntimeExports, r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { c5 as useGetFilialsQuery } from "./index-DfebxkL9.js";
import { E as ErrorIcon } from "./errorIcon-PZO0f1IY.js";
import { c as createStyles, au as useCombobox, g as Loader, k as Tooltip, b as Box, av as Combobox, E as TextInput } from "./vendor-mantine-h0WlM2Mj.js";
import { I as InfiniteScroll } from "./index.es-BTZFX8B-.js";
import { b as gq } from "./vendor-icons-xWhXK_Ui.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
function useLoadMore(ForceId, SkipCustomers) {
  const $ = compilerRuntimeExports.c(30);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = [];
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const [filials, setFilials] = reactExports.useState(t0);
  let t1;
  if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = {
      Count: 10,
      Offset: 0,
      Query: "",
      ForceCustomerId: void 0,
      SkipCustomers: []
    };
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  const [filters, setFilters] = reactExports.useState(t1);
  let t2;
  if ($[2] !== filters) {
    t2 = {
      ...filters
    };
    $[2] = filters;
    $[3] = t2;
  } else {
    t2 = $[3];
  }
  const {
    data,
    isLoading,
    isFetching,
    isError
  } = useGetFilialsQuery(t2);
  const total = (data == null ? void 0 : data.TotalCount) || 0;
  let t3;
  if ($[4] !== filters || $[5] !== total) {
    t3 = () => {
      if (total - filters.Offset > 0) {
        setFilters({
          ...filters,
          Offset: filters.Offset + 10
        });
      }
    };
    $[4] = filters;
    $[5] = total;
    $[6] = t3;
  } else {
    t3 = $[6];
  }
  const loadMore = t3;
  let t4;
  if ($[7] !== data || $[8] !== filials || $[9] !== filters) {
    t4 = () => {
      if (data) {
        setFilials(filters.Offset > 0 ? filials.concat(data.Items) : data.Items);
      }
    };
    $[7] = data;
    $[8] = filials;
    $[9] = filters;
    $[10] = t4;
  } else {
    t4 = $[10];
  }
  let t5;
  if ($[11] !== data) {
    t5 = [data];
    $[11] = data;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  reactExports.useEffect(t4, t5);
  let t6;
  if ($[13] !== ForceId || $[14] !== filters) {
    t6 = () => {
      if (ForceId && ForceId !== filters.ForceCustomerId) {
        setFilters((prevFilters) => ({
          ...prevFilters,
          ForceCustomerId: ForceId,
          Offset: 0
        }));
      }
    };
    $[13] = ForceId;
    $[14] = filters;
    $[15] = t6;
  } else {
    t6 = $[15];
  }
  let t7;
  if ($[16] !== ForceId) {
    t7 = [ForceId];
    $[16] = ForceId;
    $[17] = t7;
  } else {
    t7 = $[17];
  }
  reactExports.useEffect(t6, t7);
  let t8;
  let t9;
  if ($[18] !== SkipCustomers) {
    t8 = () => {
      if (SkipCustomers && (SkipCustomers == null ? void 0 : SkipCustomers.length)) {
        setFilters((prevFilters_0) => ({
          ...prevFilters_0,
          SkipCustomers: [...SkipCustomers],
          Offset: 0
        }));
      }
    };
    t9 = [SkipCustomers];
    $[18] = SkipCustomers;
    $[19] = t8;
    $[20] = t9;
  } else {
    t8 = $[19];
    t9 = $[20];
  }
  reactExports.useEffect(t8, t9);
  let t10;
  if ($[21] !== filials) {
    t10 = filials == null ? void 0 : filials.map(_temp);
    $[21] = filials;
    $[22] = t10;
  } else {
    t10 = $[22];
  }
  const list = t10;
  let t11;
  if ($[23] !== isError || $[24] !== isFetching || $[25] !== isLoading || $[26] !== list || $[27] !== loadMore || $[28] !== total) {
    t11 = {
      list,
      total,
      isLoading,
      isFetching,
      isError,
      loadMore
    };
    $[23] = isError;
    $[24] = isFetching;
    $[25] = isLoading;
    $[26] = list;
    $[27] = loadMore;
    $[28] = total;
    $[29] = t11;
  } else {
    t11 = $[29];
  }
  return t11;
}
function _temp(Item) {
  return {
    label: String(Item.Title),
    value: Item.Id.toString()
  };
}
const useStyles = createStyles((theme) => ({
  scrolled: {
    boxShadow: theme.shadows.sm
  },
  infiniteScroll: {
    "&::-webkit-scrollbar": {
      width: "8px",
      height: "10px"
    },
    "&::-webkit-scrollbar-thumb": {
      borderRadius: "8px",
      background: "#98A2B3"
    },
    "&::-webkit-scrollbar-track": {
      background: "#F9FAFB"
    },
    scrollbarColor: "#98A2B3 #F9FAFB"
  },
  infiniteScrollMoreData: {
    width: "100%",
    height: "50px",
    display: "flex",
    alignItems: "center",
    justifyContent: "center"
  },
  error: {
    height: "100vh",
    width: "100%",
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    flexDirection: "column",
    backgroundColor: theme.colors.backgroundGray[0],
    "& p": {
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      margin: "0px 10px"
    }
  },
  selectInput: {
    cursor: "pointer",
    "& .mantine-Input-input": {
      borderRadius: "8px"
    }
  },
  options: {
    "& .mantine-Combobox-option": {
      "&[data-combobox-active=true]": {
        backgroundColor: theme.colors.blue[5],
        color: theme.white
      }
    }
  }
}));
const SelectWithPagination = (t0) => {
  const $ = compilerRuntimeExports.c(94);
  const {
    label,
    placeholder,
    defaultValue,
    value,
    form,
    handleChange,
    fieldName,
    isCreateUser,
    isRoleComponent,
    SkipCustomers,
    className,
    filialName,
    customerField,
    disabled
  } = t0;
  const {
    list,
    isLoading,
    isFetching,
    isError,
    total,
    loadMore
  } = useLoadMore(defaultValue || value || void 0, SkipCustomers);
  const {
    t
  } = useTranslation();
  const {
    classes,
    cx
  } = useStyles();
  const combobox = useCombobox({
    onDropdownClose: () => combobox.resetSelectedOption()
  });
  let t1;
  if ($[0] !== customerField || $[1] !== form || $[2] !== list || $[3] !== value) {
    t1 = () => {
      if (value) {
        const filialItem = list.find((item) => item.value === value.toString());
        if (filialItem) {
          form == null ? void 0 : form.setFieldValue(customerField, filialItem.label);
        }
      }
    };
    $[0] = customerField;
    $[1] = form;
    $[2] = list;
    $[3] = value;
    $[4] = t1;
  } else {
    t1 = $[4];
  }
  let t2;
  if ($[5] !== list || $[6] !== value) {
    t2 = [value, list];
    $[5] = list;
    $[6] = value;
    $[7] = t2;
  } else {
    t2 = $[7];
  }
  reactExports.useEffect(t1, t2);
  let t3;
  if ($[8] !== list || $[9] !== value) {
    let t42;
    if ($[11] !== value) {
      t42 = (item_0) => /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Option, { value: item_0.value, active: item_0.value === (value == null ? void 0 : value.toString()), children: item_0.label }, item_0.value);
      $[11] = value;
      $[12] = t42;
    } else {
      t42 = $[12];
    }
    t3 = list.map(t42);
    $[8] = list;
    $[9] = value;
    $[10] = t3;
  } else {
    t3 = $[10];
  }
  const options = t3;
  let t4;
  if ($[13] !== fieldName || $[14] !== form || $[15] !== handleChange || $[16] !== isCreateUser || $[17] !== isRoleComponent) {
    t4 = (query) => {
      if (form) {
        form == null ? void 0 : form.setFieldValue(fieldName, query ? Number(query) : null);
        if (isCreateUser || isRoleComponent) {
          form == null ? void 0 : form.setFieldValue("Quota.Id", null);
          form == null ? void 0 : form.setFieldValue("Quota.Name", "");
        }
      } else {
        handleChange && handleChange(query);
      }
    };
    $[13] = fieldName;
    $[14] = form;
    $[15] = handleChange;
    $[16] = isCreateUser;
    $[17] = isRoleComponent;
    $[18] = t4;
  } else {
    t4 = $[18];
  }
  const handleChangeParentFilials = t4;
  let t5;
  if ($[19] !== fieldName || $[20] !== form || $[21] !== list[0] || $[22] !== list.length || $[23] !== value) {
    t5 = () => {
      if (list.length && value === 0) {
        form == null ? void 0 : form.setFieldValue(fieldName, list[0].value ? Number(list[0].value) : null);
        form == null ? void 0 : form.resetDirty({
          Customer: {
            Id: Number(list[0].value)
          }
        });
      }
    };
    $[19] = fieldName;
    $[20] = form;
    $[21] = list[0];
    $[22] = list.length;
    $[23] = value;
    $[24] = t5;
  } else {
    t5 = $[24];
  }
  let t6;
  if ($[25] !== list || $[26] !== value) {
    t6 = [list, value];
    $[25] = list;
    $[26] = value;
    $[27] = t6;
  } else {
    t6 = $[27];
  }
  reactExports.useEffect(t5, t6);
  let t7;
  if ($[28] !== combobox || $[29] !== isError || $[30] !== isFetching || $[31] !== list.length || $[32] !== t) {
    t7 = isFetching ? /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "xs" }) : isError ? /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { color: "red", label: t("profile.failedToLoadData"), children: /* @__PURE__ */ jsxRuntimeExports.jsx(ErrorIcon, {}) }) : list.length > 0 ? /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { size: "1rem", color: "#98A2B3", onClick: () => combobox.toggleDropdown() }) : null;
    $[28] = combobox;
    $[29] = isError;
    $[30] = isFetching;
    $[31] = list.length;
    $[32] = t;
    $[33] = t7;
  } else {
    t7 = $[33];
  }
  const rightSectionContent = t7;
  let t8;
  if ($[34] !== list || $[35] !== loadMore || $[36] !== total) {
    t8 = {
      list,
      total,
      loadMore
    };
    $[34] = list;
    $[35] = loadMore;
    $[36] = total;
    $[37] = t8;
  } else {
    t8 = $[37];
  }
  let t9;
  if ($[38] !== combobox || $[39] !== handleChangeParentFilials) {
    t9 = (val) => {
      handleChangeParentFilials(val);
      combobox.closeDropdown();
    };
    $[38] = combobox;
    $[39] = handleChangeParentFilials;
    $[40] = t9;
  } else {
    t9 = $[40];
  }
  let t10;
  if ($[41] !== list.length || $[42] !== placeholder || $[43] !== t) {
    t10 = list.length ? placeholder : t("noData");
    $[41] = list.length;
    $[42] = placeholder;
    $[43] = t;
    $[44] = t10;
  } else {
    t10 = $[44];
  }
  const t11 = filialName ?? "";
  let t12;
  if ($[45] !== className || $[46] !== classes.selectInput || $[47] !== cx) {
    t12 = cx(className, classes.selectInput);
    $[45] = className;
    $[46] = classes.selectInput;
    $[47] = cx;
    $[48] = t12;
  } else {
    t12 = $[48];
  }
  let t13;
  if ($[49] !== combobox) {
    t13 = () => combobox.toggleDropdown();
    $[49] = combobox;
    $[50] = t13;
  } else {
    t13 = $[50];
  }
  let t14;
  if ($[51] !== label || $[52] !== t) {
    t14 = label ? label : t("profile.quota");
    $[51] = label;
    $[52] = t;
    $[53] = t14;
  } else {
    t14 = $[53];
  }
  let t15;
  if ($[54] !== fieldName || $[55] !== form) {
    t15 = form == null ? void 0 : form.getInputProps(fieldName).error;
    $[54] = fieldName;
    $[55] = form;
    $[56] = t15;
  } else {
    t15 = $[56];
  }
  let t16;
  if ($[57] !== disabled || $[58] !== rightSectionContent || $[59] !== t10 || $[60] !== t11 || $[61] !== t12 || $[62] !== t13 || $[63] !== t14 || $[64] !== t15) {
    t16 = /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Target, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { disabled, placeholder: t10, readOnly: true, value: t11, className: t12, pointer: true, required: true, rightSection: rightSectionContent, onClick: t13, label: t14, error: t15 }) });
    $[57] = disabled;
    $[58] = rightSectionContent;
    $[59] = t10;
    $[60] = t11;
    $[61] = t12;
    $[62] = t13;
    $[63] = t14;
    $[64] = t15;
    $[65] = t16;
  } else {
    t16 = $[65];
  }
  const t17 = !list.length;
  const t18 = list == null ? void 0 : list.length;
  const t19 = list.length <= 5 ? "auto" : "170px";
  const t20 = list ? total > (list == null ? void 0 : list.length) : false;
  let t21;
  if ($[66] !== classes.infiniteScrollMoreData || $[67] !== list.length) {
    t21 = Boolean(list == null ? void 0 : list.length) && /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.infiniteScrollMoreData, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "sm" }) });
    $[66] = classes.infiniteScrollMoreData;
    $[67] = list.length;
    $[68] = t21;
  } else {
    t21 = $[68];
  }
  let t22;
  if ($[69] !== t21) {
    t22 = /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: t21 });
    $[69] = t21;
    $[70] = t22;
  } else {
    t22 = $[70];
  }
  let t23;
  if ($[71] !== classes.infiniteScroll || $[72] !== loadMore || $[73] !== options || $[74] !== t18 || $[75] !== t19 || $[76] !== t20 || $[77] !== t22) {
    t23 = /* @__PURE__ */ jsxRuntimeExports.jsx(InfiniteScroll, { scrollableTarget: "scrollableDiv", dataLength: t18, next: loadMore, height: t19, hasMore: t20, loader: t22, className: classes.infiniteScroll, children: options });
    $[71] = classes.infiniteScroll;
    $[72] = loadMore;
    $[73] = options;
    $[74] = t18;
    $[75] = t19;
    $[76] = t20;
    $[77] = t22;
    $[78] = t23;
  } else {
    t23 = $[78];
  }
  let t24;
  if ($[79] !== classes.options || $[80] !== t23) {
    t24 = /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Options, { className: classes.options, children: t23 });
    $[79] = classes.options;
    $[80] = t23;
    $[81] = t24;
  } else {
    t24 = $[81];
  }
  let t25;
  if ($[82] !== t17 || $[83] !== t24) {
    t25 = /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Dropdown, { hidden: t17, children: t24 });
    $[82] = t17;
    $[83] = t24;
    $[84] = t25;
  } else {
    t25 = $[84];
  }
  let t26;
  if ($[85] !== combobox || $[86] !== isLoading || $[87] !== t16 || $[88] !== t25 || $[89] !== t9) {
    t26 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Combobox, { store: combobox, position: "bottom", withinPortal: false, zIndex: 1100, onOptionSubmit: t9, "data-cy": "selectFilial", disabled: isLoading, children: [
      t16,
      t25
    ] });
    $[85] = combobox;
    $[86] = isLoading;
    $[87] = t16;
    $[88] = t25;
    $[89] = t9;
    $[90] = t26;
  } else {
    t26 = $[90];
  }
  let t27;
  if ($[91] !== t26 || $[92] !== t8) {
    t27 = /* @__PURE__ */ jsxRuntimeExports.jsx(SelectContext.Provider, { value: t8, children: t26 });
    $[91] = t26;
    $[92] = t8;
    $[93] = t27;
  } else {
    t27 = $[93];
  }
  return t27;
};
const SelectContext = reactExports.createContext({
  list: [],
  total: 0,
  loadMore: () => {
  }
});
export {
  SelectWithPagination as S,
  useLoadMore as u
};
//# sourceMappingURL=index-CfN7Ll0p.js.map
