import _typeof from "@babel/runtime/helpers/typeof";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

var _dec, _dec2, _class, _class2, _temp;

import React, { PureComponent } from 'react';
import { withTheme } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import AutoComplete from '../../jsx/IntegrationReactSelect';
import Paper from '@material-ui/core/Paper';
import ActionSearch from '@material-ui/icons/Search';
import Button from '@material-ui/core/Button';
import ActionDeleteForever from '@material-ui/icons/DeleteForever';
import TrashBox from './TrashBox';
import search, { getOptions } from './search';
import DesktopFile from './DesktopFile';
import withFiles from '../../store/withFiles';
var SearchBarHeight = 40;
var cn = {
  icon: style({
    marginTop: 4
  })
};

var getCn = function getCn(props) {
  return {
    root: style({
      display: 'flex',
      alignItems: 'center',
      boxSizing: 'border-box',
      width: '100%',
      height: SearchBarHeight,
      paddingRight: 16,
      paddingLeft: props.theme.spacing.unit,
      zIndex: 100
    }),
    bar: style({
      display: 'flex',
      alignItems: 'center',
      width: '100%',
      height: SearchBarHeight,
      paddingLeft: props.theme.spacing.unit,
      backgroundColor: props.theme.palette.background.paper
    }),
    empty: style({
      flex: '1 0 auto',
      height: SearchBarHeight,
      marginLeft: props.theme.spacing.unit
    })
  };
};

var SearchBar = (_dec = withTheme(), _dec2 = withFiles({
  includeTrashedFiles: true
}), _dec(_class = _dec2(_class = (_temp = _class2 =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(SearchBar, _PureComponent);

  function SearchBar() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, SearchBar);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(SearchBar)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      showTrashes: false,
      query: ''
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleUpdate", function (value, context) {
      var query = _typeof(value) === 'object' ? value.value : value;

      if (!query && context) {
        if (context.action !== 'input-change') {
          // react-select は value="" でイベント発火することがある
          return;
        }
      }

      var options = getOptions(query);

      _this.props.setFilter(function (file) {
        return search(file, query, options);
      });

      _this.setState({
        query: query,
        showTrashes: options.showTrashes
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTrashBoxTap", function () {
      var _this$state = _this.state,
          query = _this$state.query,
          showTrashes = _this$state.showTrashes;

      if (!showTrashes) {
        _this.handleUpdate(':trash ' + query);
      } else {
        _this.handleUpdate(query.replace(/(^|\s):trash(\s|$)/g, '$1'));
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleDelete", function () {
      var files = _this.props.files.filter(_this.props.filter);

      _this.props.deleteFiles(files);
    });

    return _this;
  }

  _createClass(SearchBar, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.handleUpdate('');
    }
  }, {
    key: "render",
    value: function render() {
      var dcn = getCn(this.props);
      var _this$props = this.props,
          onOpen = _this$props.onOpen,
          localization = _this$props.localization;
      var _this$state2 = this.state,
          showTrashes = _this$state2.showTrashes,
          query = _this$state2.query;
      var fileNames = this.props.files.map(function (f) {
        return f.moduleName;
      }).filter(function (s) {
        return s;
      }).map(function (s) {
        return {
          value: s,
          label: s
        };
      });
      return React.createElement("div", {
        className: dcn.root
      }, React.createElement(TrashBox, {
        showTrashes: showTrashes,
        onClick: this.handleTrashBoxTap
      }), React.createElement(DesktopFile, {
        onOpen: onOpen
      }), React.createElement(Paper, {
        elevation: 3,
        className: dcn.bar
      }, React.createElement(ActionSearch, {
        className: cn.icon
      }), React.createElement(AutoComplete, {
        value: query,
        suggestions: fileNames,
        onChange: this.handleUpdate,
        placeholder: ""
      })), showTrashes ? React.createElement(Button, {
        variant: "contained",
        color: "secondary",
        className: dcn.empty,
        onClick: this.handleDelete
      }, React.createElement(ActionDeleteForever, null), localization.hierarchyCard.emptyTrashBox) : null);
    }
  }]);

  return SearchBar;
}(PureComponent), _defineProperty(_class2, "propTypes", {
  theme: PropTypes.object.isRequired,
  filter: PropTypes.func.isRequired,
  setFilter: PropTypes.func.isRequired,
  onOpen: PropTypes.func.isRequired,
  localization: PropTypes.object.isRequired,
  // Injected by withFiles (for JSX)
  files: PropTypes.array.isRequired,
  deleteFiles: PropTypes.func.isRequired
}), _temp)) || _class) || _class);
export { SearchBar as default };