"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const throttleDebounce = require("throttle-debounce");
const kit_Button = require("../Button.cjs");
const kit_Input = require("../Input.cjs");
const kit_internal_functions = require("../internal/functions.cjs");
const kit_internal_types = require("../internal/types.cjs");
const kit_Row = require("../Row.cjs");
const kit_Select = require("../Select.cjs");
const ARIA_LABEL_RESET = "Reset";
const LABELS = {
  agentIds: "Agents",
  allocationIds: "Allocations",
  containerIds: "Containers",
  levels: "Levels",
  rankIds: "Ranks",
  searchText: "Searches"
};
const LogViewerSelect = ({
  onChange,
  onReset,
  options,
  showSearch,
  values
}) => {
  var _a, _b, _c, _d, _e;
  const [filters, setFilters] = React.useState(values);
  const selectOptions = React.useMemo(() => {
    const { agentIds, allocationIds, containerIds, rankIds } = options;
    return {
      ...options,
      agentIds: agentIds == null ? void 0 : agentIds.sort(kit_internal_functions.alphaNumericSorter),
      allocationIds: allocationIds == null ? void 0 : allocationIds.sort(kit_internal_functions.alphaNumericSorter),
      containerIds: containerIds == null ? void 0 : containerIds.sort(kit_internal_functions.alphaNumericSorter),
      levels: Object.entries(kit_internal_types.LogLevelFromApi).filter((entry) => entry[1] !== kit_internal_types.LogLevelFromApi.Unspecified).map(([key, value]) => ({ label: key, value })),
      rankIds: rankIds ? [-1].concat(rankIds).sort(kit_internal_functions.alphaNumericSorter) : [-1]
    };
  }, [options]);
  const moreThanOne = React.useMemo(() => {
    return Object.keys(selectOptions).reduce(
      (acc, key) => {
        const filterKey = key;
        const options2 = selectOptions[filterKey];
        acc[filterKey] = !!(options2 && options2.length > 1);
        return acc;
      },
      {}
    );
  }, [selectOptions]);
  const isResetShown = React.useMemo(() => {
    if (values.searchText)
      return true;
    const keys = Object.keys(selectOptions);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      const value = values[key];
      if (value && value.length !== 0)
        return true;
    }
    return false;
  }, [selectOptions, values]);
  const throttledChangeFilter = React.useMemo(
    () => throttleDebounce.throttle(
      500,
      (f) => {
        onChange == null ? void 0 : onChange(f);
      },
      { noLeading: true }
    ),
    [onChange]
  );
  React.useEffect(() => {
    return () => {
      throttledChangeFilter.cancel();
    };
  }, [throttledChangeFilter]);
  const handleChange = React.useCallback(
    (key, caster) => (value) => {
      setFilters((prev) => {
        const newF = {
          ...prev,
          [key]: value.map((item) => caster(item))
        };
        throttledChangeFilter(newF);
        return newF;
      });
    },
    [throttledChangeFilter]
  );
  const handleSearch = React.useCallback(
    (e) => {
      setFilters((prev) => {
        const newF = { ...prev, searchText: e.target.value };
        throttledChangeFilter(newF);
        return newF;
      });
    },
    [throttledChangeFilter]
  );
  const handleReset = React.useCallback(() => {
    setFilters({});
    onReset == null ? void 0 : onReset();
    throttledChangeFilter({});
  }, [onReset, throttledChangeFilter]);
  return /* @__PURE__ */ jsxRuntime.jsxs(kit_Row.Row, { children: [
    showSearch && /* @__PURE__ */ jsxRuntime.jsx(kit_Input, { placeholder: "Search Logs...", value: filters.searchText, onChange: handleSearch }),
    moreThanOne.allocationIds && /* @__PURE__ */ jsxRuntime.jsx(
      kit_Select.default,
      {
        disableTags: true,
        mode: "multiple",
        placeholder: `All ${LABELS.allocationIds}`,
        value: filters.allocationIds,
        width: 120,
        onChange: handleChange("allocationIds", String),
        children: (_a = selectOptions == null ? void 0 : selectOptions.allocationIds) == null ? void 0 : _a.map((id, index) => /* @__PURE__ */ jsxRuntime.jsx(kit_Select.Option, { value: id, children: id || "No Allocation ID" }, id || `no-id-${index}`))
      }
    ),
    !!((_b = selectOptions == null ? void 0 : selectOptions.agentIds) == null ? void 0 : _b.length) && /* @__PURE__ */ jsxRuntime.jsx(
      kit_Select.default,
      {
        disableTags: true,
        mode: "multiple",
        placeholder: `All ${LABELS.agentIds}`,
        value: filters.agentIds,
        width: 120,
        onChange: handleChange("agentIds", String),
        children: (_c = selectOptions == null ? void 0 : selectOptions.agentIds) == null ? void 0 : _c.map((id, index) => /* @__PURE__ */ jsxRuntime.jsx(kit_Select.Option, { value: id, children: id || "No Agent ID" }, id || `no-id-${index}`))
      }
    ),
    moreThanOne.containerIds && /* @__PURE__ */ jsxRuntime.jsx(
      kit_Select.default,
      {
        disableTags: true,
        mode: "multiple",
        placeholder: `All ${LABELS.containerIds}`,
        value: filters.containerIds,
        width: 120,
        onChange: handleChange("containerIds", String),
        children: (_d = selectOptions == null ? void 0 : selectOptions.containerIds) == null ? void 0 : _d.map((id, index) => /* @__PURE__ */ jsxRuntime.jsx(kit_Select.Option, { value: id, children: id || "No Container ID" }, id || `no-id-${index}`))
      }
    ),
    moreThanOne.rankIds && /* @__PURE__ */ jsxRuntime.jsx(
      kit_Select.default,
      {
        disableTags: true,
        mode: "multiple",
        placeholder: `All ${LABELS.rankIds}`,
        value: filters.rankIds,
        width: 120,
        onChange: handleChange("rankIds", Number),
        children: (_e = selectOptions == null ? void 0 : selectOptions.rankIds) == null ? void 0 : _e.map((id, index) => /* @__PURE__ */ jsxRuntime.jsx(kit_Select.Option, { value: id, children: id === -1 ? "No Rank" : id }, id ?? `no-id-${index}`))
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(
      kit_Select.default,
      {
        disableTags: true,
        mode: "multiple",
        placeholder: `All ${LABELS.levels}`,
        value: filters.levels,
        width: 120,
        onChange: handleChange("levels", String),
        children: selectOptions == null ? void 0 : selectOptions.levels.map((level) => /* @__PURE__ */ jsxRuntime.jsx(kit_Select.Option, { value: level.value, children: level.label }, level.value))
      }
    ),
    isResetShown && /* @__PURE__ */ jsxRuntime.jsx(kit_Button, { onClick: handleReset, children: ARIA_LABEL_RESET })
  ] });
};
exports.ARIA_LABEL_RESET = ARIA_LABEL_RESET;
exports.LABELS = LABELS;
exports.default = LogViewerSelect;
