"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _objectWithoutProperties2=_interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")),_extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_assertThisInitialized2=_interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")),_inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits")),_possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")),_getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")),_defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty")),React=_interopRequireWildcard(require("react")),_classnames=_interopRequireDefault(require("classnames")),_idx=_interopRequireDefault(require("idx")),_downshift=_interopRequireDefault(require("downshift")),_propTypes=_interopRequireDefault(require("prop-types")),_withStyles=_interopRequireDefault(require("../../hoc/withStyles")),_ErrorBoundary=_interopRequireDefault(require("../../hoc/ErrorBoundary")),_TextField=_interopRequireDefault(require("../TextField")),_styles=_interopRequireDefault(require("./styles"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);enumerableOnly&&(symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable})),keys.push.apply(keys,symbols)}return keys}function _objectSpread(target){for(var source,i=1;i<arguments.length;i++)source=null==arguments[i]?{}:arguments[i],i%2?ownKeys(Object(source),!0).forEach(function(key){(0,_defineProperty2["default"])(target,key,source[key])}):Object.getOwnPropertyDescriptors?Object.defineProperties(target,Object.getOwnPropertyDescriptors(source)):ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key))});return target}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function(){var result,Super=(0,_getPrototypeOf2["default"])(Derived);if(hasNativeReflectConstruct){var NewTarget=(0,_getPrototypeOf2["default"])(this).constructor;result=Reflect.construct(Super,arguments,NewTarget)}else result=Super.apply(this,arguments);return(0,_possibleConstructorReturn2["default"])(this,result)}}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}var SearchInput=/*#__PURE__*/function(_React$Component){function SearchInput(){var _this;(0,_classCallCheck2["default"])(this,SearchInput);for(var _len=arguments.length,args=Array(_len),_key=0;_key<_len;_key++)args[_key]=arguments[_key];return _this=_super.call.apply(_super,[this].concat(args)),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"_handleStateReducer",function(state,changes){switch(changes.type){// Downshift by default reset the input value on blur when there is no item selected
// we dont want to reset that value
case _downshift["default"].stateChangeTypes.mouseUp:case _downshift["default"].stateChangeTypes.blurInput:return _objectSpread(_objectSpread({},changes),{},{inputValue:state.inputValue});default:return changes;}}),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"_renderItem",function(_ref){var getItemProps=_ref.getItemProps,item=_ref.item,index=_ref.index,selectedItem=_ref.selectedItem,_this$props=_this.props,classes=_this$props.classes,renderItem=_this$props.renderItem,labelKey=_this$props.labelKey;return/*#__PURE__*/React.createElement("div",(0,_extends2["default"])({},getItemProps({item:item}),{key:"".concat(item[labelKey],"-").concat(index),className:(0,_classnames["default"])(classes.searchItem,{isSelected:selectedItem===item})}),renderItem?renderItem(item):item[labelKey])}),_this}(0,_inherits2["default"])(SearchInput,_React$Component);var _super=_createSuper(SearchInput);return(0,_createClass2["default"])(SearchInput,[{key:"render",value:function render(){var _this2=this,_this$props2=this.props,classes=_this$props2.classes,onSearch=_this$props2.onSearch,onItemClick=_this$props2.onItemClick,searchLoading=_this$props2.searchLoading,_this$props2$items=_this$props2.items,items=void 0===_this$props2$items?[]:_this$props2$items,showSearchIcon=_this$props2.showSearchIcon,defaultInputValue=_this$props2.defaultInputValue,renderItem=_this$props2.renderItem,labelKey=_this$props2.labelKey,showClearIcon=_this$props2.showClearIcon,onInputChange=_this$props2.onInputChange,rest=(0,_objectWithoutProperties2["default"])(_this$props2,["classes","onSearch","onItemClick","searchLoading","items","showSearchIcon","defaultInputValue","renderItem","labelKey","showClearIcon","onInputChange"]);return/*#__PURE__*/React.createElement(_ErrorBoundary["default"],null,/*#__PURE__*/React.createElement("div",{className:classes.root},/*#__PURE__*/React.createElement(_downshift["default"],{itemToString:function itemToString(i){return i?i[labelKey]:i},onChange:onItemClick,initialInputValue:defaultInputValue,stateReducer:this._handleStateReducer},function(_ref2){var getInputProps=_ref2.getInputProps,getItemProps=_ref2.getItemProps,isOpen=_ref2.isOpen,inputValue=_ref2.inputValue,selectedItem=_ref2.selectedItem,clearSelection=_ref2.clearSelection;return/*#__PURE__*/React.createElement("div",{style:{position:"relative"}},/*#__PURE__*/React.createElement("div",{className:(0,_classnames["default"])(classes.component,isOpen&&0<items.length?"is-open":"")},/*#__PURE__*/React.createElement(_TextField["default"],getInputProps(_objectSpread(_objectSpread({},rest),{},{value:inputValue||"",showClearIcon:showClearIcon||!1,label:"",classes:{textInputWrapper:classes.input},onChange:function onChange(event){var value=(0,_idx["default"])(event,function(_){return _.target.value});"undefined"==typeof value||((""===value||!value)&&clearSelection(),onInputChange(value))}}))),showSearchIcon&&/*#__PURE__*/React.createElement("div",{className:(0,_classnames["default"])(classes.icon,{isLoading:searchLoading}),onClick:function onClick(){return onSearch(inputValue)}},/*#__PURE__*/React.createElement("i",{className:(0,_classnames["default"])({"ion-search":!searchLoading,"ion-load-c fa-spin":searchLoading})}))),isOpen&&0<(items||[]).length&&/*#__PURE__*/React.createElement("div",{className:classes.searchedItemsWrapper},items.map(function(item,index){return _this2._renderItem({item:item,index:index,getItemProps:getItemProps,selectedItem:selectedItem})})))})))}}]),SearchInput}(React.Component);(0,_defineProperty2["default"])(SearchInput,"propTypes",_objectSpread(_objectSpread({},_TextField["default"].propTypes),{},{items:_propTypes["default"].arrayOf(_propTypes["default"].any),onSearch:_propTypes["default"].func,onInputChange:_propTypes["default"].func,onItemClick:_propTypes["default"].func,searchLoading:_propTypes["default"].bool,showSearchIcon:_propTypes["default"].bool,defaultInputValue:_propTypes["default"].string,renderItem:_propTypes["default"].func,labelKey:_propTypes["default"].string})),(0,_defineProperty2["default"])(SearchInput,"defaultProps",{items:[],showSearchIcon:!0,onSearch:function onSearch(){},onInputChange:function onInputChange(){},onItemClick:function onItemClick(){},defaultInputValue:"",labelKey:"label"});var _default=(0,_withStyles["default"])(_styles["default"])(SearchInput);exports["default"]=_default;