"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _reactRouter = require("react-router");

var _get = _interopRequireDefault(require("lodash/get"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _idx = _interopRequireDefault(require("idx"));

var _ButtonBase = _interopRequireDefault(require("@material-ui/core/ButtonBase"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _reactRouterDom = require("react-router-dom");

var _urls = _interopRequireDefault(require("../../config/urls"));

var _types = require("../../config/types");

var _helpers = require("../../helpers");

var _withStyles = _interopRequireDefault(require("../../hoc/withStyles"));

var _ErrorBoundary = _interopRequireDefault(require("../../hoc/ErrorBoundary"));

var _Collapse = _interopRequireDefault(require("@material-ui/core/Collapse"));

var _kudooIcon = _interopRequireDefault(require("../../assets/images/kudoo-icon.png"));

var _styles = _interopRequireDefault(require("./styles"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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; }

var Drawer =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Drawer, _React$Component);

  function Drawer(props) {
    var _this;

    _classCallCheck(this, Drawer);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Drawer).call(this, props));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "items", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "closeDrawer", function () {
      _this.setState({
        closed: true,
        userMoreClosed: true
      });

      if (_this.props.onClose) {
        _this.props.onClose();
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "openDrawer", function () {
      _this.setState({
        closed: false
      });

      if (_this.props.onOpen) {
        _this.props.onOpen();
      }
    });

    var isTablet = (0, _helpers.mobileAndTabletcheck)();

    if (isTablet) {
      if (_this.props.onClose) {
        _this.props.onClose();
      }
    }

    _this.state = {
      closed: isTablet,
      userMoreClosed: true
    };
    return _this;
  }

  _createClass(Drawer, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      if (this.props.closed !== prevProps.closed) {
        this.setState({
          closed: this.props.closed
        });
      }
    }
  }, {
    key: "_renderHamburgerIcon",
    value: function _renderHamburgerIcon() {
      var classes = this.props.classes;
      return React.createElement("div", {
        className: classes.hamburgerIconWrapper,
        onClick: this.openDrawer
      }, React.createElement("span", {
        className: classes.hamburgerIcon
      }, React.createElement("i", {
        className: "icon icon-hamburger"
      })));
    }
  }, {
    key: "_renderCloseIcon",
    value: function _renderCloseIcon() {
      var classes = this.props.classes;
      return React.createElement("div", {
        className: classes.closeIconWrapper
      }, React.createElement("span", {
        className: classes.closeIcon,
        onClick: this.closeDrawer
      }, React.createElement("i", {
        className: "icon icon-close"
      })));
    }
  }, {
    key: "_renderCompany",
    value: function _renderCompany() {
      var _this2 = this;

      var name = this.props.selectedCompany.name;
      var _this$props = this.props,
          classes = _this$props.classes,
          selectedCompany = _this$props.selectedCompany;
      var _this$state = this.state,
          closed = _this$state.closed,
          userMoreClosed = _this$state.userMoreClosed;
      var rightArrowClass = (0, _classnames.default)('icon-chevron-right', classes.moreRightArrow, {
        down: !userMoreClosed
      });
      return React.createElement("div", {
        className: classes.userWrapper
      }, closed && React.createElement("div", {
        className: classes.userImageWrapper
      }, React.createElement("div", {
        className: classes.userInitial
      }, (0, _helpers.getFirstLetters)(name))), !closed && React.createElement("div", {
        className: classes.usernameWrapper,
        onClick: function onClick() {
          _this2.setState({
            userMoreClosed: !_this2.state.userMoreClosed
          });
        }
      }, React.createElement("div", {
        className: classes.selectedCompanyName
      }, name), React.createElement("i", {
        className: rightArrowClass
      })), React.createElement(_Collapse.default, {
        component: "div",
        className: classes.userMoreWrapper,
        in: !userMoreClosed,
        timeout: "auto",
        unmountOnExit: true
      }, this.props.companies.map(function (comp, index) {
        return React.createElement(_ButtonBase.default, {
          classes: {
            root: (0, _classnames.default)(classes.userMoreItem, {
              selected: comp.id === selectedCompany.id
            })
          },
          style: {
            width: '100%'
          },
          key: index,
          onClick: function onClick() {
            _this2.props.onCompanyClick(comp);

            _this2.setState({
              userMoreClosed: true
            });
          }
        }, React.createElement("div", {
          className: classes.companyName
        }, comp.name), comp.id === selectedCompany.id && React.createElement("div", {
          className: classes.onlineStatus
        }));
      }), React.createElement(_reactRouterDom.Link, {
        to: _urls.default.MANAGE_COMPANIES(),
        onClick: function onClick() {
          _this2.setState({
            userMoreClosed: true
          });
        },
        style: {
          justifyContent: 'center'
        },
        className: classes.userMoreItem
      }, React.createElement("div", {
        className: classes.manageCompanyBtn
      }, "Manage Companies"))));
    }
  }, {
    key: "_renderItems",
    value: function _renderItems() {
      var _this$props2 = this.props,
          classes = _this$props2.classes,
          renderMenuItem = _this$props2.renderMenuItem,
          menuItems = _this$props2.menuItems;
      return menuItems.map(function (item, index) {
        var dom = React.createElement(React.Fragment, null, React.createElement("div", {
          className: classes.drawerItem,
          key: index
        }, React.createElement("div", {
          className: classes.itemTitle
        }, item.menuItem)));

        if (renderMenuItem) {
          dom = renderMenuItem(item.menuItem);
        }

        return dom;
      });
    }
  }, {
    key: "_renderDrawer",
    value: function _renderDrawer() {
      var closed = this.state.closed;
      var classes = this.props.classes;
      return React.createElement("div", {
        className: classes.wrapper
      }, React.createElement("div", {
        className: classes.upperPart
      }, closed && this._renderHamburgerIcon(), !closed && this._renderCloseIcon(), this._renderCompany(), this._renderItems()), React.createElement("div", {
        className: classes.kudooIconWraper
      }, React.createElement("img", {
        src: _kudooIcon.default,
        className: (0, _classnames.default)(classes.kudooIconImage, {
          closed: closed
        })
      })));
    }
  }, {
    key: "render",
    value: function render() {
      var classes = this.props.classes;
      var drawerComponentClasses = (0, _classnames.default)(classes.component, {
        closed: this.state.closed
      });
      return React.createElement(_ErrorBoundary.default, null, React.createElement("div", {
        className: drawerComponentClasses
      }, this._renderDrawer()));
    }
  }]);

  return Drawer;
}(React.Component);

_defineProperty(Drawer, "propTypes", _objectSpread({}, _types.withStylesProps, _types.withRouterProps, {
  closed: _propTypes.default.bool,
  onClose: _propTypes.default.func,
  onOpen: _propTypes.default.func,
  selectedCompany: _propTypes.default.object,
  companies: _propTypes.default.arrayOf(_propTypes.default.object),
  onCompanyClick: _propTypes.default.func,
  renderMenuItem: _propTypes.default.func,
  menuItems: _propTypes.default.array
}));

_defineProperty(Drawer, "defaultProps", {
  companies: [],
  selectedCompany: {},
  closed: false,
  onClose: function onClose() {},
  onOpen: function onOpen() {},
  onCompanyClick: function onCompanyClick() {}
});

var _default = (0, _reactRouter.withRouter)((0, _withStyles.default)(_styles.default)(Drawer));

exports.default = _default;