import React from 'react';
import _ from 'lodash';
import Select from 'react-select';
import defaultStyles from './styles';

export const SelectComponent = ({
  options = [],
  value,
  onChange,
  closeMenuOnSelect,
  customWrap, //used so that editor selects remains with old theme , remove when whole theme is same !
  ...restProps
}) => {
  const darkMode = localStorage.getItem('darkMode') === 'true';
  const {
    isMulti = false,
    styles = {},
    isLoading = false,
    hasSearch = true,
    height,
    width,
    placeholder = '选择..',
    customOption = undefined,
    defaultValue = null,
    useMenuPortal = true, // todo: deprecate this prop, use menuPortalTarget instead
    maxMenuHeight = 250,
    menuPortalTarget = null,
    menuPlacement = 'auto',
    useCustomStyles = false,
    isDisabled = false,
  } = restProps;

  const customStyles = useCustomStyles ? styles : defaultStyles(darkMode, width, height, styles);
  const selectOptions =
    Array.isArray(options) && options.length === 0
      ? options
      : options.map((option) => {
        if (!option.hasOwnProperty('label')) {
          return _.mapKeys(option, (value, key) => (key === 'value' ? key : 'label'));
        }
        return option;
      });

  const currentValue = selectOptions.find((option) => option.value === value) || value;

  const selectPinYin = (option, input) => {
    if (input === '') return true
    if (option.label.indexOf(input) >= 0) return true
    else if (input.charCodeAt() >= 32 && input.charCodeAt() <= 126) {
      if (option.label.spell('first').toLowerCase().indexOf(input.toLowerCase()) >= 0) return true
      else if (option.label.spell().toLowerCase().indexOf(input.toLowerCase()) >= 0) return true
      else return false
    } else {
      return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
  };

  const handleOnChange = (data) => {
    if (isMulti) {
      onChange(data);
    } else {
      onChange(data.value);
    }
  };

  const renderCustomOption = (option) => {
    if (customOption) {
      return customOption(option);
    }

    return option.label;
  };

  return (
    <Select
      {...restProps}
      defaultValue={defaultValue}
      isLoading={isLoading}
      isDisabled={isDisabled || isLoading}
      options={selectOptions}
      value={currentValue}
      isSearchable={hasSearch}
      onChange={handleOnChange}
      placeholder={placeholder}
      styles={customStyles}
      formatOptionLabel={(option) => renderCustomOption(option)}
      menuPlacement={menuPlacement}
      maxMenuHeight={maxMenuHeight}
      menuPortalTarget={useMenuPortal ? document.body : menuPortalTarget}
      closeMenuOnSelect={closeMenuOnSelect ?? true}
      classNamePrefix={`${darkMode && 'dark-theme'} ${customWrap && 'react-select'}`}
      filterOption={selectPinYin}
      noOptionsMessage={() => '未找到'}
    />
  );
};
