import _extends from "@babel/runtime/helpers/extends";
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, _class, _class2, _temp;

import React, { PureComponent } from 'react';
import { style } from 'typestyle';
import PropTypes from 'prop-types';
import Button from '@material-ui/core/Button';
import ContentAdd from '@material-ui/icons/Add';
import includes from 'lodash/includes';
import Card from '../CardWindow';
import CardFloatingBar from '../CardFloatingBar';
import { AddDialog } from '../../FileDialog/';
import Root from './Root';
import SearchBar from './SearchBar';
import ImmutableFile from '../../File/ImmutableFile';
import withFiles from '../../store/withFiles';
var cn = {
  button: style({
    margin: 16,
    alignSelf: 'flex-end'
  })
};
var HierarchyCard = (_dec = withFiles({
  includeTrashedFiles: true
}), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(HierarchyCard, _PureComponent);

  function HierarchyCard() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, HierarchyCard);

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      openAddDialog: false,
      openedPaths: [''],
      filter: function filter() {
        return false;
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleNativeDrop", function (files) {
      var dir = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
      Promise.all(files.map(ImmutableFile.fromFile)).then(function (files) {
        files = files.map(function (file) {
          return dir ? file.move(dir.path) : file;
        });

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleDirToggle", function (dir) {
      var openedPaths = _this.isDirOpened(dir, _this.state.openedPaths.filter(function (path) {
        return path !== dir.path;
      }), _this.state.openedPaths.concat(dir.path));

      _this.setState({
        openedPaths: openedPaths
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleFileMove", function (file, dir) {
      _this.props.deleteFiles([file]);

      return _this.props.putFile(file.move(dir.path));
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleFileSelect", function (file) {
      _this.props.globalEvent.emit('message.editor', {
        data: {
          value: file.name
        }
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "isDirOpened", function (dir, passed, failed) {
      return includes(_this.state.openedPaths, dir.path) ? passed : failed;
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleAdd", function () {
      _this.setState({
        openAddDialog: true
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleClose", function () {
      _this.setState({
        openAddDialog: false
      });
    });

    return _this;
  }

  _createClass(HierarchyCard, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          files = _this$props.files,
          openFileDialog = _this$props.openFileDialog,
          localization = _this$props.localization;
      var filter = this.state.filter;
      var transfer = {
        openFileDialog: openFileDialog,
        isDirOpened: this.isDirOpened,
        handleFileSelect: this.handleFileSelect,
        handleDirToggle: this.handleDirToggle,
        handleFileMove: this.handleFileMove,
        handleNativeDrop: this.handleNativeDrop
      };
      return React.createElement(Card, _extends({}, this.props.cardPropsBag, {
        fit: true
      }), React.createElement(CardFloatingBar, null, this.props.localization.hierarchyCard.title), React.createElement(SearchBar, {
        filter: this.state.filter,
        setFilter: function setFilter(filter) {
          return _this2.setState({
            filter: filter
          });
        },
        onOpen: this.handleNativeDrop,
        openFileDialog: openFileDialog,
        localization: localization
      }), React.createElement(Root, _extends({
        showingFiles: files.filter(filter)
      }, transfer)), React.createElement(Button, {
        variant: "contained",
        mini: true,
        className: cn.button,
        onClick: this.handleAdd
      }, React.createElement(ContentAdd, null)), React.createElement(AddDialog, {
        localization: this.props.localization,
        open: this.state.openAddDialog,
        onClose: this.handleClose
      }));
    }
  }]);

  return HierarchyCard;
}(PureComponent), _defineProperty(_class2, "propTypes", {
  openFileDialog: PropTypes.func.isRequired,
  localization: PropTypes.object.isRequired,
  cardPropsBag: PropTypes.object.isRequired,
  globalEvent: PropTypes.object.isRequired,
  // Injected by withFiles (for JSX)
  files: PropTypes.array.isRequired,
  putFile: PropTypes.func.isRequired,
  putFiles: PropTypes.func.isRequired,
  deleteFiles: PropTypes.func.isRequired
}), _temp)) || _class);
export { HierarchyCard as default };