function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import { __rest } from "tslib";
import React, { createElement, forwardRef, useEffect, useState, useRef } from "react";
import classNames from 'classnames';
import { Text } from "@alifd/meet-react-component-one";
import { isWeb } from '@uni/env';
import View from '../view';
import Icon from '../icon';
import Input from '../input';
import { useLocale } from '../locale';
import defaultLang from '../locale/lang/zh-cn';
import { useDeprecated, useValue } from '../utils/hooks';
import { isFunction, isString } from '../utils';

var SearchTag = function SearchTag(props) {
  var children = props.children,
      _props$inForm = props.inForm,
      inForm = _props$inForm === void 0 ? false : _props$inForm,
      _props$onSubmit = props.onSubmit,
      _onSubmit = _props$onSubmit === void 0 ? function () {} : _props$onSubmit,
      others = __rest(props, ["children", "inForm", "onSubmit"]);

  if (isWeb && !inForm) {
    return /*#__PURE__*/React.createElement("form", _extends({}, others, {
      action: "#",
      onSubmit: function onSubmit(e) {
        e.preventDefault();

        _onSubmit();
      }
    }), children);
  } else {
    return /*#__PURE__*/React.createElement(View, others, children);
  }
};

var Search = function Search(props, _ref) {
  var _classNames;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      _props$size = props.size,
      size = _props$size === void 0 ? 'medium' : _props$size,
      valueProp = props.value,
      placeholder = props.placeholder,
      defaultValue = props.defaultValue,
      searchText = props.searchText,
      _props$icon = props.icon,
      icon = _props$icon === void 0 ? 'search' : _props$icon,
      _props$keyboardType = props.keyboardType,
      keyboardType = _props$keyboardType === void 0 ? 'web-search' : _props$keyboardType,
      maxLength = props.maxLength,
      _props$enableNative = props.enableNative,
      enableNative = _props$enableNative === void 0 ? false : _props$enableNative,
      _props$focused = props.focused,
      focused = _props$focused === void 0 ? false : _props$focused,
      _props$highlightable = props.highlightable,
      highlightable = _props$highlightable === void 0 ? false : _props$highlightable,
      _props$hasIcon = props.hasIcon,
      hasIcon = _props$hasIcon === void 0 ? true : _props$hasIcon,
      _props$hasClear = props.hasClear,
      hasClear = _props$hasClear === void 0 ? false : _props$hasClear,
      _props$hasCancel = props.hasCancel,
      hasCancel = _props$hasCancel === void 0 ? false : _props$hasCancel,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      className = props.className,
      cancelText = props.cancelText,
      onChange = props.onChange,
      _onConfirm = props.onConfirm,
      _onSearch = props.onSearch,
      _props$onBlur = props.onBlur,
      onBlur = _props$onBlur === void 0 ? function () {} : _props$onBlur,
      _props$onFocus = props.onFocus,
      onFocus = _props$onFocus === void 0 ? function () {} : _props$onFocus,
      _props$onCancel = props.onCancel,
      onCancel = _props$onCancel === void 0 ? function () {} : _props$onCancel,
      onClear = props.onClear,
      others = __rest(props, ["prefix", "size", "value", "placeholder", "defaultValue", "searchText", "icon", "keyboardType", "maxLength", "enableNative", "focused", "highlightable", "hasIcon", "hasClear", "hasCancel", "disabled", "className", "cancelText", "onChange", "onConfirm", "onSearch", "onBlur", "onFocus", "onCancel", "onClear"]);

  var clsPrefix = "".concat(prefix, "search");
  var rcRef = useRef(null);
  var ref = _ref || rcRef;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      isFocused = _useState2[0],
      setFocused = _useState2[1];

  var _useState3 = useState(true),
      _useState4 = _slicedToArray(_useState3, 2),
      isInForm = _useState4[0],
      setInForm = _useState4[1];

  var locale = useLocale('Search', props, defaultLang.Search);

  var _useValue = useValue(props, ''),
      _useValue2 = _slicedToArray(_useValue, 3),
      value = _useValue2[0],
      setValue = _useValue2[1],
      isControlled = _useValue2[2];

  var clses = {
    search: classNames(className, (_classNames = {}, _defineProperty(_classNames, "".concat(clsPrefix), true), _defineProperty(_classNames, "".concat(clsPrefix, "--").concat(size), true), _defineProperty(_classNames, "".concat(clsPrefix, "--highlighted"), highlightable && isFocused), _classNames))
  };

  var handleChange = function handleChange(v, e) {
    if (!isControlled) {
      setValue(v);
    }

    if (onChange) {
      onChange(v, e);
    }
  };

  var renderIcon = function renderIcon() {
    var searchIcon = icon;

    if (!hasIcon) {
      searchIcon = null;
    } else if (isString(icon)) {
      searchIcon = /*#__PURE__*/React.createElement(Icon, {
        name: icon,
        className: classNames("".concat(prefix, "search--icon"), "".concat(prefix, "search--icon-").concat(size))
      });
    }

    return searchIcon;
  };

  useEffect(function () {
    if (isWeb && ref && ref.current) {
      var el = ref.current;
      var _inForm = false;

      while (el.parentNode && el.parentNode !== el) {
        if (el.parentNode.tagName && el.parentNode.tagName.toLowerCase() === 'form') {
          _inForm = true;
          break;
        }

        el = el.parentNode;
      }

      setInForm(_inForm);
    }
  }, []);
  var onSearch = useDeprecated('Search', props, 'onConfirm', 'onSearch');

  var handleSubmit = function handleSubmit(v) {
    if (onSearch) {
      onSearch(v);
    }
  };

  var handleWebSubmit = function handleWebSubmit() {
    if (isWeb) {
      var inputEl = ref.current.getElementsByTagName('input');

      if (inputEl && inputEl[0] && inputEl[0].value) {
        handleSubmit(inputEl[0].value);
      } else {
        handleSubmit('');
      }
    }
  };

  var handleFocus = function handleFocus() {
    setFocused(true);

    if (isFunction(onFocus)) {
      onFocus();
    }
  };

  var handleBlur = function handleBlur() {
    setFocused(false);

    if (isFunction(onBlur)) {
      onBlur();
    }
  };

  var focusProp = Object.assign({}, focused ? {
    focused: true
  } : null);
  return /*#__PURE__*/React.createElement(SearchTag, _extends({}, others, {
    inForm: isInForm,
    className: clses.search,
    onSubmit: handleWebSubmit
  }), /*#__PURE__*/React.createElement(View, {
    ref: ref,
    className: classNames(className, "".concat(prefix, "search--wrapper"))
  }, renderIcon(), /*#__PURE__*/React.createElement(Input, _extends({}, focusProp, {
    className: classNames("".concat(prefix, "search-input"), "".concat(prefix, "search-input--").concat(size)),
    composition: true,
    confirmType: "search",
    value: value,
    size: size,
    placeholder: placeholder || locale.searchTipText,
    keyboardType: keyboardType,
    disabled: disabled,
    maxLength: maxLength,
    hasClear: hasClear,
    enableNative: enableNative,
    onChange: handleChange,
    onConfirm: handleSubmit,
    onFocus: handleFocus,
    onBlur: handleBlur,
    onClear: onClear
  }))), hasCancel ? /*#__PURE__*/React.createElement(Text, {
    onClick: onCancel,
    className: classNames("".concat(prefix, "search--").concat(size, "-cancel"))
  }, cancelText || locale.cancelText) : null);
};

Search.displayName = 'Search';
export default /*#__PURE__*/forwardRef(Search);