import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Check } from "@truststack/icons-ui";
import { useEffect, useState } from "react";
import { FlatList } from "react-native";
import { View, YStack } from "tamagui";
import { Divider } from "./Divider.native.js";
import { ListItem } from "./List.native.js";
import { ListFooter } from "./ListFooter.native.js";
import { SearchBar } from "./Search.native.js";
import { useDebounce } from "./utils";
function SearchList(param) {
  var {
      value,
      onChange,
      searchFunc,
      placeholder = "Search..."
    } = param,
    [searching, setSearching] = useState(void 0),
    [search, setSearch] = useState(""),
    [results, setResults] = useState([]),
    debounce = useDebounce(search, 1e3);
  return useEffect(function () {
    debounce?.length && (setSearching(!0), searchFunc(debounce).then(setResults).finally(function () {
      return setSearching(!1);
    }));
  }, [debounce]), /* @__PURE__ */_jsxs(YStack, {
    gap: 12,
    children: [/* @__PURE__ */_jsx(View, {
      margin: "$spacing.margin_compact",
      children: /* @__PURE__ */_jsx(SearchBar, {
        placeholder,
        value: search,
        onChangeText: setSearch
      })
    }), /* @__PURE__ */_jsx(FlatList, {
      data: results,
      ItemSeparatorComponent: Divider,
      ListFooterComponent: /* @__PURE__ */_jsx(ListFooter, {
        loading: searching,
        noResults: !!search && searching === !1 && !results?.length
      }),
      renderItem: function (param2) {
        var {
          item
        } = param2;
        return /* @__PURE__ */_jsxs(ListItem, {
          pressable: !0,
          line: "one",
          onPress: function () {
            return onChange(item.value);
          },
          children: [/* @__PURE__ */_jsx(ListItem.Container, {
            children: /* @__PURE__ */_jsx(ListItem.Headline, {
              adjustsFontSizeToFit: !0,
              numberOfLines: 1,
              children: item.label
            })
          }), item.value == value && /* @__PURE__ */_jsx(ListItem.TrailingIcon, {
            Icon: Check
          })]
        });
      }
    })]
  });
}
export { SearchList };
//# sourceMappingURL=SearchList.native.js.map
