"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapStateToProps = exports.TechBarsComponent = exports.TechBars = void 0;
var _react = _interopRequireWildcard(require("react"));
var _store = require("../redux/store");
var _recompose = require("recompose");
var _bundleCommon = require("@kineticdata/bundle-common");
var _PageTitle = require("./shared/PageTitle");
var _router = require("@reach/router");
var _reactstrap = require("reactstrap");
var _techBarApp = require("../redux/modules/techBarApp");
var _constants = require("../constants");
var _react2 = require("@kineticdata/react");
var TechBarsComponent = exports.TechBarsComponent = function TechBarsComponent(_ref) {
  var kapp = _ref.kapp,
    techBars = _ref.techBars,
    openDropdown = _ref.openDropdown,
    toggleDropdown = _ref.toggleDropdown,
    hasTechBarDisplayRole = _ref.hasTechBarDisplayRole,
    currentTechBar = _ref.currentTechBar,
    selectCurrentTechBar = _ref.selectCurrentTechBar;
  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-container page-container--tech-bar page-container--lg"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-panel"
  }, /*#__PURE__*/_react["default"].createElement(_PageTitle.PageTitle, {
    parts: ['All Tech Bars'],
    breadcrumbs: [{
      label: 'Home',
      to: '/'
    }, {
      label: 'Tech Bar',
      to: '..'
    }],
    title: "All Tech Bars"
  }), /*#__PURE__*/_react["default"].createElement("section", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "cards cards--thirds"
  }, techBars.map(function (techBar) {
    return /*#__PURE__*/_react["default"].createElement(_bundleCommon.Card, {
      key: techBar.id,
      bar: true,
      barSize: "xs",
      barColor: "subtle",
      barStyle: currentTechBar && techBar.id === currentTechBar.id ? {
        borderTopColor: _bundleCommon.Constants.COLORS.sunflower
      } : undefined
    }, /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardCol, null, /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, null, /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "prepend"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "fa fa-map-marker fa-3x"
    })), /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardCol, null, /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, null, /*#__PURE__*/_react["default"].createElement("strong", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.values['Name']))), techBar.values['Location'] && /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "meta",
      className: "text-muted pt-0"
    }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.values['Location']))), /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "append"
    }, hasTechBarDisplayRole(techBar.values['Name']) && /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "append"
    }, /*#__PURE__*/_react["default"].createElement(_reactstrap.Dropdown, {
      toggle: toggleDropdown(techBar.id),
      isOpen: openDropdown === techBar.id
    }, /*#__PURE__*/_react["default"].createElement(_reactstrap.DropdownToggle, {
      color: "link",
      className: "btn-sm"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "fa fa-ellipsis-v fa-2x"
    })), /*#__PURE__*/_react["default"].createElement(_reactstrap.DropdownMenu, {
      right: true
    }, /*#__PURE__*/_react["default"].createElement(_router.Link, {
      to: "../display/".concat(techBar.values['Id'], "/checkin"),
      onClick: toggleDropdown(techBar.id),
      className: "dropdown-item",
      target: "_blank"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "fa fa-fw fa-external-link mr-2"
    }), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Check In"))), /*#__PURE__*/_react["default"].createElement(_router.Link, {
      to: "../display/".concat(techBar.values['Id'], "/feedback"),
      onClick: toggleDropdown(techBar.id),
      className: "dropdown-item",
      target: "_blank"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "fa fa-external-link fa-fw mr-2"
    }), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Feedback"))), /*#__PURE__*/_react["default"].createElement(_router.Link, {
      to: "../display/".concat(techBar.values['Id'], "/checkin?crosslink"),
      onClick: toggleDropdown(techBar.id),
      className: "dropdown-item",
      target: "_blank"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "fa fa-external-link fa-fw mr-2"
    }), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Check In"), " / ", /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Feedback"))), /*#__PURE__*/_react["default"].createElement(_router.Link, {
      to: "../display/".concat(techBar.values['Id'], "/overhead"),
      onClick: toggleDropdown(techBar.id),
      className: "dropdown-item",
      target: "_blank"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "fa fa-external-link fa-fw mr-2"
    }), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Overhead")))))))), /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "meta",
      className: "flex-grow-1 flex-shrink-1 overflow-auto my-2"
    }, /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardCol, null, techBar.values['Description'] && /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "meta",
      className: "pt-1"
    }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.values['Description'])), techBar.values['Details'] && /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, {
      type: "meta",
      className: "pt-1"
    }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.values['Details'])))), /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardCol, {
      className: "pt-0 flex-grow-0"
    }, /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, null, /*#__PURE__*/_react["default"].createElement(_router.Link, {
      to: "../appointment/".concat(techBar.values['Id']),
      className: "btn btn-primary flex-grow-1"
    }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Schedule Now"), " \u2192")), techBars.size > 1 && /*#__PURE__*/_react["default"].createElement(_bundleCommon.CardRow, null, currentTechBar && techBar.id === currentTechBar.id ? /*#__PURE__*/_react["default"].createElement("button", {
      className: "btn btn-sm btn-success",
      disabled: true
    }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Current Tech Bar")) : /*#__PURE__*/_react["default"].createElement("button", {
      className: "btn btn-link btn-sm",
      onClick: function onClick() {
        return selectCurrentTechBar(techBar.id);
      }
    }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Set as Current Tech Bar"))))));
  }))))));
};
var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, props) {
  return {
    kapp: (0, _bundleCommon.selectCurrentKapp)(state),
    techBars: state.techBarApp.schedulers.filter(function (s) {
      return s.values['Status'] === 'Active';
    }).map(props.userLocation ? (0, _techBarApp.mapTechBarsForDistance)(props.userLocation) : function (t) {
      return t;
    }).sort(props.userLocation ? _techBarApp.sortTechBarsByDistance : function () {
      return 0;
    }),
    profile: state.app.profile
  };
};
var toggleDropdown = function toggleDropdown(_ref2) {
  var setOpenDropdown = _ref2.setOpenDropdown,
    openDropdown = _ref2.openDropdown;
  return function (dropdownSlug) {
    return function () {
      return setOpenDropdown(dropdownSlug === openDropdown ? false : dropdownSlug);
    };
  };
};
var hasTechBarDisplayRole = function hasTechBarDisplayRole(_ref3) {
  var profile = _ref3.profile;
  return function (techBarName) {
    return _bundleCommon.Utils.isMemberOf(profile, "Role::Tech Bar Display::".concat(techBarName));
  };
};
var selectCurrentTechBar = function selectCurrentTechBar(_ref4) {
  var techBars = _ref4.techBars,
    setCurrentTechBar = _ref4.setCurrentTechBar;
  return function (id) {
    sessionStorage.setItem(_constants.SESSION_ITEM_CURRENT_TECH_BAR, id);
    setCurrentTechBar(techBars.find(function (t) {
      return t.id === id;
    }));
  };
};
var TechBars = exports.TechBars = (0, _recompose.compose)((0, _recompose.withState)('userLocation', 'setUserLocation', function () {
  try {
    var locObj = JSON.parse(sessionStorage.getItem(_constants.SESSION_ITEM_USER_LOCATION));
    if (locObj && locObj.latitude != null && locObj.longitude != null) {
      return locObj;
    }
  } catch (e) {}
  return null;
}), (0, _store.connect)(mapStateToProps), (0, _recompose.withState)('currentTechBar', 'setCurrentTechBar', function (_ref5) {
  var techBars = _ref5.techBars;
  var techBarId = sessionStorage.getItem(_constants.SESSION_ITEM_CURRENT_TECH_BAR);
  return techBarId ? techBars.find(function (t) {
    return t.id === techBarId;
  }) : null;
}), (0, _recompose.withState)('openDropdown', 'setOpenDropdown', false), (0, _recompose.withHandlers)({
  toggleDropdown: toggleDropdown,
  hasTechBarDisplayRole: hasTechBarDisplayRole,
  selectCurrentTechBar: selectCurrentTechBar
}))(TechBarsComponent);