"use strict";

require("core-js/modules/web.dom.iterable");

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

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

var _reactRedux = require("react-redux");

var apiActions = _interopRequireWildcard(require("../../actions/api"));

var callTakerActions = _interopRequireWildcard(require("../../actions/call-taker"));

var fieldTripActions = _interopRequireWildcard(require("../../actions/field-trip"));

var uiActions = _interopRequireWildcard(require("../../actions/ui"));

var _config = require("../../util/config");

var _icon = _interopRequireDefault(require("../util/icon"));

var _styled = require("./styled");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

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

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

/**
 * This component displays the controls for the Call Taker/Field Trip modules,
 * including:
 *  - start/end call button
 *  - view call list
 *  - view field trip list
 */
class CallTakerControls extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_onClickCall", () => {
      if (this._callInProgress()) this.props.endCall();else this.props.beginCall();
    });

    _defineProperty(this, "_renderCallButtonIcon", () => {
      // Show stop button if call not in progress.
      if (this._callInProgress()) {
        return /*#__PURE__*/_react.default.createElement(_icon.default, {
          className: "fa-3x",
          style: {
            marginLeft: '3px'
          },
          type: "stop"
        });
      } // No call is in progress.


      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_icon.default, {
        style: {
          marginLeft: '17px',
          marginTop: '6px',
          position: 'absolute'
        },
        type: "plus"
      }), /*#__PURE__*/_react.default.createElement(_icon.default, {
        className: "fa-4x fa-flip-horizontal",
        type: "phone"
      }));
    });

    _defineProperty(this, "_callInProgress", () => Boolean(this.props.callTaker.activeCall));
  }

  componentDidUpdate(prevProps) {
    const {
      callTakerEnabled,
      fetchCalls,
      fetchFieldTrips,
      fieldTripEnabled,
      session
    } = this.props; // Once session is available, fetch calls.

    if (session && !prevProps.session) {
      if (callTakerEnabled) fetchCalls();
      if (fieldTripEnabled) fetchFieldTrips();
    }
  }

  render() {
    const {
      callTakerEnabled,
      fieldTripEnabled,
      resetAndToggleCallHistory,
      resetAndToggleFieldTrips,
      session
    } = this.props; // If no valid session is found, do not show calltaker controls.

    if (!session) return null;
    return /*#__PURE__*/_react.default.createElement(_styled.ControlsContainer, null, callTakerEnabled && /*#__PURE__*/_react.default.createElement(_styled.ToggleCallButton, {
      callInProgress: this._callInProgress(),
      className: "call-taker-button",
      onClick: this._onClickCall
    }, this._renderCallButtonIcon()), this._callInProgress() ? /*#__PURE__*/_react.default.createElement(_styled.CallTimeCounter, null) : null, callTakerEnabled && /*#__PURE__*/_react.default.createElement(_styled.CallHistoryButton, {
      className: "call-taker-button",
      onClick: resetAndToggleCallHistory
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      className: "fa-2x",
      style: {
        marginLeft: '-3px'
      },
      type: "history"
    })), fieldTripEnabled && /*#__PURE__*/_react.default.createElement(_styled.FieldTripsButton, {
      className: "call-taker-button",
      onClick: resetAndToggleFieldTrips
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      className: "fa-2x",
      style: {
        marginLeft: '2px',
        marginTop: '3px'
      },
      type: "graduation-cap"
    })));
  }

}

const mapStateToProps = (state, ownProps) => {
  return {
    callTaker: state.callTaker,
    callTakerEnabled: (0, _config.isModuleEnabled)(state, _config.Modules.CALL_TAKER),
    fieldTripEnabled: (0, _config.isModuleEnabled)(state, _config.Modules.FIELD_TRIP),
    session: state.callTaker.session
  };
};

const mapDispatchToProps = {
  beginCall: callTakerActions.beginCall,
  endCall: callTakerActions.endCall,
  fetchCalls: callTakerActions.fetchCalls,
  fetchFieldTrips: fieldTripActions.fetchFieldTrips,
  resetAndToggleCallHistory: callTakerActions.resetAndToggleCallHistory,
  resetAndToggleFieldTrips: fieldTripActions.resetAndToggleFieldTrips,
  routingQuery: apiActions.routingQuery,
  setMainPanelContent: uiActions.setMainPanelContent
};

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(CallTakerControls);

exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=call-taker-controls.js