import { jsx, jsxs } from "react/jsx-runtime";
import React__default, { useRef, useState, useCallback, useEffect, Fragment } from "react";
import default_1$1 from "../../node_modules/@mui/icons-material/Close.js";
import default_1$2 from "../../node_modules/@mui/icons-material/Search.js";
import default_1 from "../../node_modules/@mui/icons-material/Tune.js";
import moment from "moment";
import { getAssetUrl } from "../../utils/assetBase.js";
import { debounce } from "../../utils/debounce.js";
import { DatePicker } from "../DatePicker/index.js";
import { DateRangePicker } from "../DateRangePicker/index.js";
import { Input } from "../Input/index.js";
import { Select } from "../Select/index.js";
import { syncFiltersWithModal, parseInlineSearchValue, buildFilterModelEntry } from "./filterUtils.js";
import { Tooltip } from "../Tooltip/index.js";
import Badge from "../../node_modules/@mui/material/Badge/Badge.js";
const TableSortDescIcon = getAssetUrl("icons/Down.svg");
const TableSortIcon = getAssetUrl("icons/tableSortIcon.svg");
const TableSortAscIcon = getAssetUrl("icons/Up.svg");
function AgGridHeader({
  children,
  sortModel,
  handleInlineSearch,
  handleClearSearchInline,
  handleCloseInputField,
  debounceTime,
  initialFilters,
  enableHeaderTextWrap = true,
  ...rest
}) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea;
  if (!rest.column || !rest.api) {
    return null;
  }
  const getLabelFromDropdownValue = (value, columnDef) => {
    if (!(columnDef == null ? void 0 : columnDef.isThirdOptionDropdownIsBoolean) || !(columnDef == null ? void 0 : columnDef.thirdOptionDropdownOptions) || !Array.isArray(columnDef.thirdOptionDropdownOptions)) {
      return value;
    }
    const option = columnDef.thirdOptionDropdownOptions.find((opt) => {
      const optValue = typeof opt === "object" ? opt.value : opt;
      const filterValue = String(value || "").toLowerCase();
      const optValueStr = String(optValue || "").toLowerCase();
      return optValueStr === filterValue || optValueStr === filterValue.replace(/\s+/g, "");
    });
    if (option) {
      return typeof option === "object" ? option.label : option;
    }
    return value;
  };
  const getOptionFromDropdownValue = (value, columnDef) => {
    if (!(columnDef == null ? void 0 : columnDef.thirdOptionDropdownOptions) || !Array.isArray(columnDef.thirdOptionDropdownOptions) || !value) {
      return value;
    }
    const valueStr = String(value || "").toLowerCase().trim();
    if (!valueStr) {
      return value;
    }
    const option = columnDef.thirdOptionDropdownOptions.find((opt) => {
      const optValue = typeof opt === "object" ? opt.value : opt;
      const optValueStr = String(optValue || "").toLowerCase().trim();
      return optValueStr === valueStr;
    });
    if (option) {
      return typeof option === "object" ? option : { label: option, value: option };
    }
    return value;
  };
  const menuRef = useRef();
  const inputRef = useRef();
  useRef();
  const [showInputField, setShowInputField] = useState(false);
  const [isTextTruncated, setIsTextTruncated] = useState(false);
  const [searchText, setSearchText] = useState("");
  const [menuVisible, setMenuvisible] = useState(false);
  const [columnWidth, setColumnWidth] = useState({});
  const [currentSort, setCurrentSort] = useState(((_b = (_a = rest.column) == null ? void 0 : _a.getSort) == null ? void 0 : _b.call(_a)) ?? null);
  const [hasActiveSearch, setHasActiveSearch] = useState(false);
  const [selectedDate, setSelectedDate] = useState(null);
  const [showDatePicker, setShowDatePicker] = useState(false);
  const [isInputFocused, setIsInputFocused] = useState(false);
  const originalSuppressHeaderKeyboardEventRef = useRef(
    (_d = (_c = rest.column) == null ? void 0 : _c.colDef) == null ? void 0 : _d.suppressHeaderKeyboardEvent
  );
  const filterModel = rest.api.getFilterModel();
  const isFilterAppliedChanged = filterModel[(_e = rest.column) == null ? void 0 : _e.colId];
  const [isOverflowText, setIsOverflowText] = useState(false);
  const headerTextRef = useRef(null);
  const containerRef = useRef(null);
  const [containerHeight, setContainerHeight] = useState(0);
  const onFilterChanged = useCallback(() => {
    var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i2, _j2, _k2, _l2, _m2, _n2, _o2, _p2;
    const newFilterModel = (_a2 = rest.api) == null ? void 0 : _a2.getFilterModel();
    const columnId = ((_b2 = rest.column) == null ? void 0 : _b2.field) || ((_c2 = rest.column) == null ? void 0 : _c2.colId);
    const columnFilter = newFilterModel == null ? void 0 : newFilterModel[columnId];
    let filterValue = "";
    if (columnFilter) {
      if (((_e2 = (_d2 = rest.column) == null ? void 0 : _d2.colDef) == null ? void 0 : _e2.filter) === "agDateColumnFilter") {
        const dateFrom = columnFilter == null ? void 0 : columnFilter.dateFrom;
        const dateTo = columnFilter == null ? void 0 : columnFilter.dateTo;
        if (dateFrom && dateTo) {
          setStartDate(moment(dateFrom));
          setEndDate(moment(dateTo));
          const dateFromStr = dateFrom instanceof Date ? moment(dateFrom).format("YYYY-MM-DD") : typeof dateFrom === "string" ? dateFrom.split(" ")[0] : dateFrom;
          const dateToStr = dateTo instanceof Date ? moment(dateTo).format("YYYY-MM-DD") : typeof dateTo === "string" ? dateTo.split(" ")[0] : dateTo;
          filterValue = `${dateFromStr} - ${dateToStr}`;
        } else if (dateFrom) {
          filterValue = moment(dateFrom);
          setSelectedDate(filterValue);
          if (dateFrom instanceof Date) {
            filterValue = moment(dateFrom).format("YYYY-MM-DD");
          } else if (typeof dateFrom === "string") {
            filterValue = dateFrom.split(" ")[0];
          } else {
            filterValue = dateFrom;
          }
        }
      } else {
        filterValue = columnFilter == null ? void 0 : columnFilter.filter;
        if (((_g2 = (_f2 = rest.column) == null ? void 0 : _f2.colDef) == null ? void 0 : _g2.thirdOptionAsDropdown) && filterValue) {
          const dropdownOptions = (_i2 = (_h2 = rest.column) == null ? void 0 : _h2.colDef) == null ? void 0 : _i2.thirdOptionDropdownOptions;
          const option = Array.isArray(dropdownOptions) ? dropdownOptions.find((opt) => {
            const optValue = typeof opt === "object" ? opt.value : opt;
            return String(optValue) === String(filterValue);
          }) : null;
          if (option) {
            setSelectedDropdownOption(
              typeof option === "object" ? option : { label: option, value: option }
            );
          }
        } else {
          const displayValue = getLabelFromDropdownValue(filterValue, (_j2 = rest.column) == null ? void 0 : _j2.colDef);
          setSearchText(displayValue);
        }
      }
      const gridModel = (_l2 = (_k2 = rest.api) == null ? void 0 : _k2.getModel) == null ? void 0 : _l2.call(_k2);
      const advancedFilterModel = gridModel == null ? void 0 : gridModel.advancedFilterModel;
      if (advancedFilterModel && ((_m2 = rest.column) == null ? void 0 : _m2.colId) && advancedFilterModel[rest.column.colId]) ;
    }
    if (!filterValue) {
      setSearchText("");
      setSelectedDropdownOption(null);
      setSelectedDate(null);
      setStartDate(null);
      setEndDate(null);
      setHasActiveSearch(false);
    } else {
      if (!((_o2 = (_n2 = rest.column) == null ? void 0 : _n2.colDef) == null ? void 0 : _o2.thirdOptionAsDropdown)) {
        const displayValue = getLabelFromDropdownValue(filterValue, (_p2 = rest.column) == null ? void 0 : _p2.colDef);
        setSearchText(displayValue);
      }
      setHasActiveSearch(true);
    }
  }, [
    rest.api,
    (_f = rest.column) == null ? void 0 : _f.colId,
    (_g = rest.column) == null ? void 0 : _g.field,
    (_i = (_h = rest.column) == null ? void 0 : _h.colDef) == null ? void 0 : _i.thirdOptionDropdownOptions
  ]);
  useEffect(() => {
    var _a2, _b2, _c2, _d2;
    if ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.thirdOptionAsDropdown) {
      const options = ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.thirdOptionDropdownOptions) || [];
      setCurrentDropdownOptions(options);
    }
  }, [(_k = (_j = rest.column) == null ? void 0 : _j.colDef) == null ? void 0 : _k.thirdOptionAsDropdown, (_m = (_l = rest.column) == null ? void 0 : _l.colDef) == null ? void 0 : _m.thirdOptionDropdownOptions]);
  const [startDate, setStartDate] = useState(null);
  const [endDate, setEndDate] = useState(null);
  const [isDropdownOpen, setIsDropdownOpen] = useState(false);
  const [selectedDropdownOption, setSelectedDropdownOption] = useState(null);
  const [currentDropdownOptions, setCurrentDropdownOptions] = useState([]);
  useEffect(() => {
    var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i2, _j2;
    const filterModel2 = rest.api.getFilterModel();
    if (filterModel2[(_a2 = rest.column) == null ? void 0 : _a2.colId]) {
      const columnFilter = filterModel2[((_b2 = rest.column) == null ? void 0 : _b2.field) || ((_c2 = rest.column) == null ? void 0 : _c2.colId)];
      let filterValue;
      if (((_e2 = (_d2 = rest.column) == null ? void 0 : _d2.colDef) == null ? void 0 : _e2.filter) === "agDateColumnFilter") {
        const dateFrom = columnFilter == null ? void 0 : columnFilter.dateFrom;
        const dateTo = columnFilter == null ? void 0 : columnFilter.dateTo;
        if (dateFrom && dateTo) {
          setStartDate(moment(dateFrom));
          setEndDate(moment(dateTo));
          filterValue = `${dateFrom} - ${dateTo}`;
        } else if (dateFrom) {
          filterValue = moment(dateFrom);
          setSelectedDate(filterValue);
        }
      } else {
        filterValue = columnFilter == null ? void 0 : columnFilter.filter;
        if (((_g2 = (_f2 = rest.column) == null ? void 0 : _f2.colDef) == null ? void 0 : _g2.thirdOptionAsDropdown) && filterValue) {
          const options = ((_i2 = (_h2 = rest.column) == null ? void 0 : _h2.colDef) == null ? void 0 : _i2.thirdOptionDropdownOptions) || [];
          const option = options.find((opt) => {
            const optValue = typeof opt === "object" ? opt.value : opt;
            return String(optValue) === String(filterValue);
          });
          if (option) {
            setSelectedDropdownOption(
              typeof option === "object" ? option : { label: option, value: option }
            );
          }
        } else {
          const displayValue = getLabelFromDropdownValue(filterValue, (_j2 = rest.column) == null ? void 0 : _j2.colDef);
          setSearchText(displayValue);
        }
      }
      setHasActiveSearch(!!filterValue);
    } else {
      setSearchText("");
      setSelectedDropdownOption(null);
      setSelectedDate(null);
      setStartDate(null);
      setEndDate(null);
      setHasActiveSearch(false);
    }
    rest.api.addEventListener("filterChanged", onFilterChanged);
    return () => {
      rest.api.removeEventListener("filterChanged", onFilterChanged);
    };
  }, [isFilterAppliedChanged, onFilterChanged, currentDropdownOptions]);
  const handleSearchChange = (value) => {
    var _a2;
    if (handleInlineSearch) {
      handleInlineSearch == null ? void 0 : handleInlineSearch(value);
      (_a2 = rest.onFilterChanged) == null ? void 0 : _a2.call(rest, rest);
      return;
    }
    setTimeout(() => {
      var _a3, _b2, _c2, _d2;
      try {
        const { inputValue, filterType } = parseSearchValue(value);
        applyFilterToGrid(inputValue, filterType);
        syncFiltersWithModal(
          rest.api,
          rest.column.colId,
          inputValue,
          (_a3 = rest.column) == null ? void 0 : _a3.colDef,
          rest.colDefs,
          rest.setValue,
          rest.setInitialFilters,
          getOptionFromDropdownValue
        );
        (_c2 = (_b2 = rest.api) == null ? void 0 : _b2.onFilterChanged) == null ? void 0 : _c2.call(_b2);
        (_d2 = rest.onFilterChanged) == null ? void 0 : _d2.call(rest, rest);
      } catch (error) {
        console.warn("Error during filter operation:", error);
      }
    }, 0);
  };
  const parseSearchValue = (value) => {
    var _a2, _b2, _c2, _d2, _e2;
    if ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.thirdOptionAsDropdown) {
      const isBoolean = ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.dataType) === "boolean";
      return {
        inputValue: value,
        filterType: isBoolean ? "equals" : "contains"
      };
    } else {
      const parsed = parseInlineSearchValue(
        value,
        (_e2 = rest.column) == null ? void 0 : _e2.colDef,
        rest.api,
        rest.column.colId
      );
      return {
        inputValue: parsed.parsedValue,
        filterType: parsed.filterType
      };
    }
  };
  const applyFilterToGrid = (inputValue, filterType) => {
    var _a2;
    const currentFilterModel = rest.sortModel || rest.api.getFilterModel();
    const filterEntry = buildFilterModelEntry(inputValue, filterType, (_a2 = rest.column) == null ? void 0 : _a2.colDef);
    if (filterEntry === void 0) {
      delete currentFilterModel[rest.column.colId];
    } else {
      currentFilterModel[rest.column.colId] = filterEntry;
    }
    rest.api.setFilterModel(currentFilterModel);
    setTimeout(() => {
      if (rest.api) {
        const displayedRowCount = rest.api.getDisplayedRowCount();
        if (displayedRowCount === 0) {
          rest.api.showNoRowsOverlay();
        } else {
          rest.api.hideOverlay();
        }
      }
    }, 100);
  };
  const debouncedHandleSearchChange = useCallback(
    debounce((value) => {
      handleSearchChange(value);
    }, debounceTime),
    []
  );
  const handleSearch = (e) => {
    var _a2, _b2, _c2, _d2;
    e.stopPropagation();
    if (((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.filter) === "agDateColumnFilter") {
      setShowDatePicker((prev) => !prev);
    } else if ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.thirdOptionAsDropdown) {
      setShowInputField((prev) => !prev);
      if (!showInputField) {
        setTimeout(() => {
          setIsDropdownOpen(true);
        }, 0);
      }
    } else {
      setShowInputField((prev) => !prev);
    }
    setColumnWidth((prev) => {
      var _a3, _b3;
      return {
        ...prev,
        [(_a3 = rest.column) == null ? void 0 : _a3.colId]: (_b3 = rest.column) == null ? void 0 : _b3.actualWidth
      };
    });
  };
  const onCloseIconClick = (colId) => {
    var _a2, _b2, _c2, _d2;
    setSearchText("");
    setSelectedDropdownOption(null);
    setSelectedDate(null);
    setStartDate(null);
    setEndDate(null);
    setHasActiveSearch(false);
    rest.api.destroyFilter(colId);
    const currentFilters = ((_b2 = (_a2 = rest.getValues) == null ? void 0 : _a2.call(rest)) == null ? void 0 : _b2.filters) || [];
    const updatedFilters = currentFilters.filter((f) => {
      var _a3;
      return ((_a3 = f == null ? void 0 : f.column) == null ? void 0 : _a3.value) !== colId;
    });
    (_c2 = rest.setValue) == null ? void 0 : _c2.call(rest, "filters", updatedFilters);
    (_d2 = rest.setInitialFilters) == null ? void 0 : _d2.call(rest, updatedFilters);
    if (rest.onFilterChanged) {
      rest.onFilterChanged(rest);
    }
  };
  const handleClickOutside = (event) => {
    setMenuvisible(false);
  };
  useEffect(() => {
    document.addEventListener("click", handleClickOutside);
    return () => {
      document.removeEventListener("click", handleClickOutside);
    };
  }, [menuVisible]);
  const onHeaderClick = () => {
    var _a2, _b2;
    if (rest.applySort) {
      rest.applySort(rest.column.colId);
      return;
    }
    if ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.sortable) {
      const currentSortModel = rest.api.getColumnState();
      const currentSort2 = currentSortModel.find((model) => model.colId === rest.column.colId);
      const newSort = (currentSort2 == null ? void 0 : currentSort2.sort) === null ? "asc" : (currentSort2 == null ? void 0 : currentSort2.sort) === "asc" ? "desc" : null;
      rest.api.applyColumnState({
        state: [{ colId: rest.column.colId, sort: newSort }],
        defaultState: { sort: null }
      });
    }
  };
  useEffect(() => {
    var _a2;
    const onSortChanged = () => {
      var _a3, _b2;
      setCurrentSort(((_b2 = (_a3 = rest.column) == null ? void 0 : _a3.getSort) == null ? void 0 : _b2.call(_a3)) ?? null);
    };
    (_a2 = rest.api) == null ? void 0 : _a2.addEventListener("sortChanged", onSortChanged);
    return () => {
      var _a3;
      return (_a3 = rest.api) == null ? void 0 : _a3.removeEventListener("sortChanged", onSortChanged);
    };
  }, [rest.api, rest.column]);
  const onColumnMoved = (params) => {
    var _a2;
    if (((_a2 = params == null ? void 0 : params.column) == null ? void 0 : _a2.colId) && (showInputField || showDatePicker)) {
      setColumnWidth((prev) => ({
        ...prev,
        [params.column.colId]: params.column.actualWidth
      }));
      rest.api.applyColumnState({
        state: [
          {
            colId: params.column.colId,
            width: params.column.actualWidth
          }
        ],
        defaultState: { width: null }
      });
    }
  };
  useEffect(() => {
    var _a2;
    if (rest) {
      (_a2 = rest.api) == null ? void 0 : _a2.addEventListener("columnResized", onColumnMoved);
    }
    return () => {
      var _a3;
      (_a3 = rest.api) == null ? void 0 : _a3.removeEventListener("columnResized", onColumnMoved);
    };
  }, [rest]);
  useEffect(() => {
    var _a2;
    const colDef = (_a2 = rest.column) == null ? void 0 : _a2.colDef;
    if (!colDef) return;
    if (isInputFocused) {
      if (originalSuppressHeaderKeyboardEventRef.current === void 0) {
        originalSuppressHeaderKeyboardEventRef.current = colDef.suppressHeaderKeyboardEvent;
      }
      colDef.suppressHeaderKeyboardEvent = () => true;
    } else {
      colDef.suppressHeaderKeyboardEvent = originalSuppressHeaderKeyboardEventRef.current;
    }
    return () => {
      if (colDef) {
        colDef.suppressHeaderKeyboardEvent = originalSuppressHeaderKeyboardEventRef.current;
      }
    };
  }, [isInputFocused, (_n = rest.column) == null ? void 0 : _n.colId]);
  useEffect(() => {
    if (containerRef.current) {
      setContainerHeight(containerRef.current.clientHeight);
    }
  }, []);
  return /* @__PURE__ */ jsx(
    "div",
    {
      ref: containerRef,
      className: `custom-ag-header-container ${((_p = (_o = rest.column) == null ? void 0 : _o.colDef) == null ? void 0 : _p.checkboxSelection) ? "checkbox-selection" : ""}`,
      onClick: () => rest.onClick(),
      children: showDatePicker ? /* @__PURE__ */ jsxs(
        "div",
        {
          className: `ia-search-input-field ${(columnWidth[(_q = rest.column) == null ? void 0 : _q.colId] ?? ((_r = rest.column) == null ? void 0 : _r.actualWidth)) < 200 ? "ia-search-input-narrow" : ""}`,
          children: [
            ((_t = (_s = rest.column) == null ? void 0 : _s.colDef) == null ? void 0 : _t.thirdOptionAsDateRangePicker) ? /* @__PURE__ */ jsx(
              DateRangePicker,
              {
                fullWidth: true,
                isAgGridCellRenderer: true,
                withPortal: true,
                customIcon: ((_v = (_u = rest.column) == null ? void 0 : _u.colDef) == null ? void 0 : _v.advanceSearchEnabled) && /* @__PURE__ */ jsx(
                  default_1,
                  {
                    className: "ia-search-input-field-custom-icon",
                    onClick: (e) => {
                      var _a2, _b2, _c2, _d2, _e2, _f2;
                      e.stopPropagation();
                      if (rest.onAdvanceSearchClick) {
                        rest.onAdvanceSearchClick(rest.column);
                        return;
                      }
                      rest.toggleAdvanceSearch(
                        ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.toShowDuplicateColumnInAdvanceSearch) ? ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.columnFieldsInAdvanceSearch) ? (_f2 = (_e2 = rest.column) == null ? void 0 : _e2.colDef) == null ? void 0 : _f2.columnFieldsInAdvanceSearch : rest.column.colId : rest.column.colId
                      );
                    }
                  }
                ),
                displayFormat: ((_x = (_w = rest.column) == null ? void 0 : _w.colDef) == null ? void 0 : _x.displayFormat) || "MM-DD-YYYY",
                endDate,
                handleDatesChange: (start, end) => {
                  setStartDate(start);
                  setEndDate(end);
                },
                maxDate: /* @__PURE__ */ new Date("2100-12-31"),
                minDate: /* @__PURE__ */ new Date("2000-01-01"),
                setEndDate,
                setStartDate,
                startDate,
                onPrimaryButtonClick: () => {
                  if (startDate && endDate) {
                    handleSearchChange({ startDate, endDate });
                  }
                  setShowDatePicker(false);
                },
                onResetClick: () => {
                  setStartDate(null);
                  setEndDate(null);
                  handleSearchChange(null);
                  setShowDatePicker(false);
                },
                onSecondaryButtonClick: () => {
                  setShowDatePicker(false);
                }
              }
            ) : /* @__PURE__ */ jsx(
              DatePicker,
              {
                fullWidth: true,
                isAgGridCellRenderer: true,
                showMonthYearSelect: true,
                withPortal: true,
                customIcon: ((_z = (_y = rest.column) == null ? void 0 : _y.colDef) == null ? void 0 : _z.advanceSearchEnabled) && /* @__PURE__ */ jsx(
                  default_1,
                  {
                    className: "ia-search-input-field-custom-icon",
                    onClick: () => {
                      var _a2, _b2, _c2, _d2, _e2, _f2;
                      if (rest.onAdvanceSearchClick) {
                        rest.onAdvanceSearchClick(rest.column);
                        return;
                      }
                      rest.toggleAdvanceSearch(
                        ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.toShowDuplicateColumnInAdvanceSearch) ? ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.columnFieldsInAdvanceSearch) ? (_f2 = (_e2 = rest.column) == null ? void 0 : _e2.colDef) == null ? void 0 : _f2.columnFieldsInAdvanceSearch : rest.column.colId : rest.column.colId
                      );
                    }
                  }
                ),
                displayFormat: ((_B = (_A = rest.column) == null ? void 0 : _A.colDef) == null ? void 0 : _B.displayFormat) || "MM-DD-YYYY",
                maxDate: /* @__PURE__ */ new Date("2100-12-31"),
                minDate: /* @__PURE__ */ new Date("2000-01-01"),
                placeholder: "Select Date",
                selectedDate,
                setSelectedDate,
                onPrimaryButtonClick: () => {
                  handleSearchChange(selectedDate);
                  setShowDatePicker(false);
                },
                onSecondaryButtonClick: () => {
                  setShowDatePicker(false);
                },
                onTertiaryButtonClick: () => {
                  setSelectedDate(null);
                  handleSearchChange(null);
                  setShowDatePicker(false);
                }
              }
            ),
            /* @__PURE__ */ jsx(
              default_1$1,
              {
                className: "ia-search-input-field-close-icon",
                style: { cursor: "pointer" },
                onClick: () => {
                  handleCloseInputField == null ? void 0 : handleCloseInputField();
                  setShowDatePicker(false);
                }
              }
            )
          ]
        }
      ) : showInputField ? /* @__PURE__ */ jsxs(
        "div",
        {
          className: `ia-search-input-field ${(columnWidth[(_C = rest.column) == null ? void 0 : _C.colId] ?? ((_D = rest.column) == null ? void 0 : _D.actualWidth)) < 200 ? "ia-search-input-narrow" : ""}`,
          children: [
            ((_F = (_E = rest.column) == null ? void 0 : _E.colDef) == null ? void 0 : _F.thirdOptionAsDropdown) && !((_H = (_G = rest.column) == null ? void 0 : _G.colDef) == null ? void 0 : _H.toShowDuplicateColumnInAdvanceSearch) ? /* @__PURE__ */ jsxs(
              "div",
              {
                id: "ia-select-option-dropdown-container",
                style: {
                  position: "relative",
                  width: selectedDropdownOption ? "66%" : "100%"
                },
                children: [
                  /* @__PURE__ */ jsx(
                    Select,
                    {
                      currentOptions: currentDropdownOptions,
                      initialOptions: currentDropdownOptions,
                      isClearable: true,
                      isOpen: isDropdownOpen,
                      placeholder: "Select option",
                      selectedOptions: selectedDropdownOption || null,
                      setCurrentOptions: setCurrentDropdownOptions,
                      setIsOpen: setIsDropdownOpen,
                      setSelectedOptions: (option) => {
                        setSelectedDropdownOption(option);
                        if (option) {
                          const value = typeof option === "object" ? option.value : option;
                          setHasActiveSearch(true);
                          handleSearchChange(value);
                        } else {
                          setHasActiveSearch(false);
                          handleSearchChange(null);
                        }
                      },
                      onClearAll: () => {
                        setSelectedDropdownOption(null);
                        setHasActiveSearch(false);
                        handleSearchChange(null);
                      },
                      withPortal: true,
                      minWidth: "100%"
                    }
                  ),
                  ((_J = (_I = rest.column) == null ? void 0 : _I.colDef) == null ? void 0 : _J.advanceSearchEnabled) && /* @__PURE__ */ jsx(
                    default_1,
                    {
                      className: "ia-search-input-field-custom-icon",
                      id: "toggle-advance-search-icon",
                      style: {
                        position: "absolute",
                        right: selectedDropdownOption ? "44px" : "28px",
                        top: "50%",
                        transform: "translateY(-50%)",
                        cursor: "pointer",
                        zIndex: 1,
                        pointerEvents: "auto"
                      },
                      onClick: (e) => {
                        var _a2, _b2, _c2, _d2, _e2, _f2;
                        e.stopPropagation();
                        if (rest.onAdvanceSearchClick) {
                          rest.onAdvanceSearchClick(rest.column);
                          return;
                        }
                        rest.toggleAdvanceSearch(
                          ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.toShowDuplicateColumnInAdvanceSearch) ? ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.columnFieldsInAdvanceSearch) ? (_f2 = (_e2 = rest.column) == null ? void 0 : _e2.colDef) == null ? void 0 : _f2.columnFieldsInAdvanceSearch : rest.column.colId : rest.column.colId
                        );
                      }
                    }
                  )
                ]
              }
            ) : /* @__PURE__ */ jsx(
              Input,
              {
                ref: inputRef,
                iconClickOnDisabled: (_L = (_K = rest.column) == null ? void 0 : _K.colDef) == null ? void 0 : _L.toShowDuplicateColumnInAdvanceSearch,
                isDisabled: (_N = (_M = rest.column) == null ? void 0 : _M.colDef) == null ? void 0 : _N.toShowDuplicateColumnInAdvanceSearch,
                placeholder: "Search",
                rightIcon: /* @__PURE__ */ jsxs(Fragment, { children: [
                  searchText.toString().length > 0 && (columnWidth[(_O = rest.column) == null ? void 0 : _O.colId] ?? ((_P = rest.column) == null ? void 0 : _P.actualWidth)) >= 200 && /* @__PURE__ */ jsx(
                    default_1$1,
                    {
                      onClick: () => {
                        if (handleClearSearchInline) {
                          handleClearSearchInline(rest);
                        }
                        onCloseIconClick(rest.column.colId);
                      }
                    }
                  ),
                  ((_R = (_Q = rest.column) == null ? void 0 : _Q.colDef) == null ? void 0 : _R.advanceSearchEnabled) && /* @__PURE__ */ jsx(
                    default_1,
                    {
                      onClick: () => {
                        var _a2, _b2, _c2, _d2, _e2, _f2;
                        if (rest.onAdvanceSearchClick) {
                          rest.onAdvanceSearchClick(rest.column);
                          return;
                        }
                        rest.toggleAdvanceSearch(
                          ((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.toShowDuplicateColumnInAdvanceSearch) ? ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.columnFieldsInAdvanceSearch) ? (_f2 = (_e2 = rest.column) == null ? void 0 : _e2.colDef) == null ? void 0 : _f2.columnFieldsInAdvanceSearch : rest.column.colId : rest.column.colId,
                          searchText
                        );
                      }
                    }
                  )
                ] }),
                value: searchText,
                onChange: (e) => {
                  setSearchText(e.target.value);
                  setHasActiveSearch(!!e.target.value);
                  debouncedHandleSearchChange(e.target.value);
                }
              }
            ),
            /* @__PURE__ */ jsx(
              default_1$1,
              {
                className: "ia-search-input-field-close-icon",
                style: { cursor: "pointer" },
                onClick: () => {
                  handleCloseInputField == null ? void 0 : handleCloseInputField();
                  setShowInputField(false);
                  setSelectedDropdownOption(null);
                }
              }
            )
          ]
        }
      ) : (
        // Original simple structure when enableHeaderTextWrap is false
        /* @__PURE__ */ jsxs(React__default.Fragment, { children: [
          /* @__PURE__ */ jsxs(
            "div",
            {
              className: `ag-header-cell-text ${((_T = (_S = rest.column) == null ? void 0 : _S.colDef) == null ? void 0 : _T.sortable) ? "sortable" : ""} ${enableHeaderTextWrap ? "header-wrap-enabled" : ""}`,
              children: [
                ((_V = (_U = rest.column) == null ? void 0 : _U.colDef) == null ? void 0 : _V.field) === "Selection" || ((_X = (_W = rest.column) == null ? void 0 : _W.colDef) == null ? void 0 : _X.checkboxSelection) ? children : /* @__PURE__ */ jsx(
                  "div",
                  {
                    ref: headerTextRef,
                    className: "header-text-content",
                    style: { WebkitLineClamp: containerHeight > 40 ? 4 : 2 },
                    onMouseEnter: () => {
                      var _a2, _b2, _c2, _d2;
                      setIsOverflowText(
                        ((_a2 = headerTextRef.current) == null ? void 0 : _a2.scrollWidth) > ((_b2 = headerTextRef.current) == null ? void 0 : _b2.clientWidth) || ((_c2 = headerTextRef.current) == null ? void 0 : _c2.scrollHeight) > ((_d2 = headerTextRef.current) == null ? void 0 : _d2.clientHeight) + 1
                      );
                    },
                    children: /* @__PURE__ */ jsx(
                      Tooltip,
                      {
                        orientation: "top",
                        title: isOverflowText && children !== "Selection" ? children : "",
                        variant: "tertiary",
                        children
                      }
                    )
                  }
                ),
                ((_Z = (_Y = rest.column) == null ? void 0 : _Y.colDef) == null ? void 0 : _Z.sortable) && /* @__PURE__ */ jsx(
                  "img",
                  {
                    alt: "TableSortIcon",
                    className: "ia-table-sort-icon",
                    src: currentSort === "asc" ? TableSortAscIcon : currentSort === "desc" ? TableSortDescIcon : TableSortIcon,
                    onClick: onHeaderClick
                  }
                )
              ]
            }
          ),
          /* @__PURE__ */ jsxs(
            "div",
            {
              className: `ia-icon-div ${hasActiveSearch ? "has-active-search" : ""} ${menuVisible ? "menu-visible" : ""}`,
              children: [
                (_aa = (_$ = (__ = rest.column) == null ? void 0 : __.colDef) == null ? void 0 : _$.customHeaderIcons) == null ? void 0 : _aa.map((icon) => {
                  return icon;
                }),
                ((_ca = (_ba = rest.column) == null ? void 0 : _ba.colDef) == null ? void 0 : _ca.isSearchable) && (hasActiveSearch || !showInputField) && /* @__PURE__ */ jsx(Badge, { invisible: !hasActiveSearch, variant: "dot", children: /* @__PURE__ */ jsx(
                  default_1$2,
                  {
                    fontSize: "small",
                    style: {
                      color: "#60697D",
                      cursor: "pointer",
                      backgroundColor: hasActiveSearch && "#ECEEFD",
                      display: menuVisible && !hasActiveSearch ? "none" : "block"
                    },
                    onClick: rest.onColumnSearchClick ? () => {
                      var _a2, _b2, _c2, _d2;
                      rest.onColumnSearchClick(rest);
                      setShowInputField(true);
                      if (((_b2 = (_a2 = rest.column) == null ? void 0 : _a2.colDef) == null ? void 0 : _b2.filter) === "agDateColumnFilter") {
                        setShowDatePicker(true);
                      } else if ((_d2 = (_c2 = rest.column) == null ? void 0 : _c2.colDef) == null ? void 0 : _d2.thirdOptionAsDropdown) {
                        setTimeout(() => {
                          setIsDropdownOpen(true);
                        }, 0);
                      }
                    } : handleSearch
                  }
                ) }),
                !((_ea = (_da = rest.column) == null ? void 0 : _da.colDef) == null ? void 0 : _ea.suppressMenu) && /* @__PURE__ */ jsx("div", { className: "custom-ag-header-icons", children: /* @__PURE__ */ jsx(
                  "button",
                  {
                    ref: menuRef,
                    className: `custom-ag-menu-icon-button`,
                    onClick: (e) => {
                      e.stopPropagation();
                      rest.showColumnMenu(menuRef.current);
                      setMenuvisible(true);
                    }
                  }
                ) })
              ]
            }
          )
        ] })
      )
    }
  );
}
export {
  AgGridHeader as default
};
