"use strict";

import { useContext, useState } from 'react';
import { FlatList, TouchableOpacity, useWindowDimensions, View } from 'react-native';
import styles from "./styles.js";
import { ApplicationContext } from "../Context/index.js";
import InputSearch from "../Input/InputSearch.js";
import { Radio } from "../Radio/index.js";
import { SizedBox } from "../SizedBox/index.js";
import { Spacing, Styles } from "../Consts/index.js";
import { Text } from "../Text/index.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const SheetPicker = ({
  data,
  selected,
  onSelect,
  renderItem,
  onRequestClose
}) => {
  const {
    height: heightDevice
  } = useWindowDimensions();
  const {
    theme,
    navigator
  } = useContext(ApplicationContext);
  const [search, setSearch] = useState('');

  /**
   * on action popup
   * @param callback
   */
  const onAction = callback => {
    if (typeof onRequestClose === 'function') {
      onRequestClose?.(callback);
    } else {
      navigator?.pop();
      setTimeout(() => {
        callback?.();
      }, 300);
    }
  };
  const defaultRenderItem = ({
    item
  }) => {
    const index = data?.findIndex(i => i.value === item.value);
    let borderColor = theme.colors.border.default;
    if (item.value === selected?.value) {
      borderColor = theme.colors.primary.default;
    }
    return /*#__PURE__*/_jsxs(TouchableOpacity, {
      onPress: () => {
        onAction(() => {
          onSelect(index);
        });
      },
      style: [styles.item, {
        borderColor,
        backgroundColor: theme.colors.background.surface
      }],
      children: [!!item.icon && /*#__PURE__*/_jsxs(_Fragment, {
        children: [item.icon, /*#__PURE__*/_jsx(SizedBox, {
          width: Spacing.S
        })]
      }), /*#__PURE__*/_jsxs(View, {
        style: Styles.flex,
        children: [/*#__PURE__*/_jsx(Text, {
          typography: 'subhead',
          fontWeight: 'bold',
          children: item.title
        }), item.subTitle && /*#__PURE__*/_jsx(Text, {
          typography: 'caption2',
          fontWeight: 'medium',
          children: item.subTitle
        })]
      }), /*#__PURE__*/_jsx(Radio, {
        value: item.value,
        groupValue: selected?.value,
        onChange: () => {
          onAction(() => {
            onSelect(index);
          });
        }
      })]
    });
  };
  return /*#__PURE__*/_jsxs(View, {
    children: [/*#__PURE__*/_jsx(View, {
      style: Styles.paddingM,
      children: /*#__PURE__*/_jsx(InputSearch, {
        placeholder: 'Search option',
        defaultValue: search,
        onChangeText: setSearch
      })
    }), /*#__PURE__*/_jsx(FlatList, {
      renderItem: renderItem ?? defaultRenderItem,
      style: {
        maxHeight: heightDevice * 0.7
      },
      contentContainerStyle: Styles.paddingHorizontalM,
      data: data.filter(item => {
        return item.title.toUpperCase().includes(search.toUpperCase());
      }),
      ItemSeparatorComponent: () => /*#__PURE__*/_jsx(View, {
        style: {
          height: Spacing.S
        }
      }),
      keyExtractor: item => item.title
    }), /*#__PURE__*/_jsx(SizedBox, {
      height: Spacing.M
    })]
  });
};
export { SheetPicker };
//# sourceMappingURL=index.js.map