import { jsxs, jsx } from "react/jsx-runtime";
import { useState, useMemo, useEffect, useCallback } from "react";
import { throttle } from "throttle-debounce";
import Button from "../Button.js";
import Input from "../Input.js";
import { alphaNumericSorter } from "../internal/functions.js";
import { LogLevelFromApi } from "../internal/types.js";
import { Row } from "../Row.js";
import Select, { Option } from "../Select.js";
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] = useState(values);
  const selectOptions = useMemo(() => {
    const { agentIds, allocationIds, containerIds, rankIds } = options;
    return {
      ...options,
      agentIds: agentIds == null ? void 0 : agentIds.sort(alphaNumericSorter),
      allocationIds: allocationIds == null ? void 0 : allocationIds.sort(alphaNumericSorter),
      containerIds: containerIds == null ? void 0 : containerIds.sort(alphaNumericSorter),
      levels: Object.entries(LogLevelFromApi).filter((entry) => entry[1] !== LogLevelFromApi.Unspecified).map(([key, value]) => ({ label: key, value })),
      rankIds: rankIds ? [-1].concat(rankIds).sort(alphaNumericSorter) : [-1]
    };
  }, [options]);
  const moreThanOne = 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 = 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 = useMemo(
    () => throttle(
      500,
      (f) => {
        onChange == null ? void 0 : onChange(f);
      },
      { noLeading: true }
    ),
    [onChange]
  );
  useEffect(() => {
    return () => {
      throttledChangeFilter.cancel();
    };
  }, [throttledChangeFilter]);
  const handleChange = useCallback(
    (key, caster) => (value) => {
      setFilters((prev) => {
        const newF = {
          ...prev,
          [key]: value.map((item) => caster(item))
        };
        throttledChangeFilter(newF);
        return newF;
      });
    },
    [throttledChangeFilter]
  );
  const handleSearch = useCallback(
    (e) => {
      setFilters((prev) => {
        const newF = { ...prev, searchText: e.target.value };
        throttledChangeFilter(newF);
        return newF;
      });
    },
    [throttledChangeFilter]
  );
  const handleReset = useCallback(() => {
    setFilters({});
    onReset == null ? void 0 : onReset();
    throttledChangeFilter({});
  }, [onReset, throttledChangeFilter]);
  return /* @__PURE__ */ jsxs(Row, { children: [
    showSearch && /* @__PURE__ */ jsx(Input, { placeholder: "Search Logs...", value: filters.searchText, onChange: handleSearch }),
    moreThanOne.allocationIds && /* @__PURE__ */ jsx(
      Select,
      {
        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__ */ jsx(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__ */ jsx(
      Select,
      {
        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__ */ jsx(Option, { value: id, children: id || "No Agent ID" }, id || `no-id-${index}`))
      }
    ),
    moreThanOne.containerIds && /* @__PURE__ */ jsx(
      Select,
      {
        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__ */ jsx(Option, { value: id, children: id || "No Container ID" }, id || `no-id-${index}`))
      }
    ),
    moreThanOne.rankIds && /* @__PURE__ */ jsx(
      Select,
      {
        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__ */ jsx(Option, { value: id, children: id === -1 ? "No Rank" : id }, id ?? `no-id-${index}`))
      }
    ),
    /* @__PURE__ */ jsx(
      Select,
      {
        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__ */ jsx(Option, { value: level.value, children: level.label }, level.value))
      }
    ),
    isResetShown && /* @__PURE__ */ jsx(Button, { onClick: handleReset, children: ARIA_LABEL_RESET })
  ] });
};
export {
  ARIA_LABEL_RESET,
  LABELS,
  LogViewerSelect as default
};
