"use strict";

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

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

var _moment = _interopRequireDefault(require("moment"));

require("moment-timezone");

var _coreUtils = _interopRequireDefault(require("@opentripplanner/core-utils"));

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

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

var _viewer = require("../../util/viewer");

var _patternRow = _interopRequireDefault(require("./pattern-row"));

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

const {
  getUserTimezone
} = _coreUtils.default.time;
const defaultState = {
  spin: false,
  timer: null
};
/**
 * Table showing next arrivals (refreshing every 10 seconds) for the specified
 * stop organized by route pattern.
 */

class LiveStopTimes extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", defaultState);

    _defineProperty(this, "_refreshStopTimes", () => {
      const {
        findStopTimesForStop,
        viewedStop
      } = this.props;
      findStopTimesForStop({
        stopId: viewedStop.stopId
      });
      this.setState({
        spin: true
      });
      window.setTimeout(this._stopSpin, 1000);
    });

    _defineProperty(this, "_onToggleAutoRefresh", () => {
      const {
        autoRefreshStopTimes,
        toggleAutoRefresh
      } = this.props;

      if (autoRefreshStopTimes) {
        toggleAutoRefresh(false);
      } else {
        // Turn on auto-refresh and refresh immediately to give user feedback.
        this._refreshStopTimes();

        toggleAutoRefresh(true);
      }
    });

    _defineProperty(this, "_stopSpin", () => this.setState({
      spin: false
    }));

    _defineProperty(this, "_startAutoRefresh", () => {
      const timer = window.setInterval(this._refreshStopTimes, 10000);
      this.setState({
        timer
      });
    });

    _defineProperty(this, "_stopAutoRefresh", () => {
      window.clearInterval(this.state.timer);
    });
  }

  componentDidMount() {
    // Turn on stop times refresh if enabled.
    if (this.props.autoRefreshStopTimes) this._startAutoRefresh();
  }

  componentWillUnmount() {
    // Turn off auto refresh unconditionally (just in case).
    this._stopAutoRefresh();
  }

  componentDidUpdate(prevProps) {
    // Handle stopping or starting the auto refresh timer.
    if (prevProps.autoRefreshStopTimes && !this.props.autoRefreshStopTimes) this._stopAutoRefresh();else if (!prevProps.autoRefreshStopTimes && this.props.autoRefreshStopTimes) this._startAutoRefresh();
  }

  render() {
    const {
      homeTimezone,
      stopData,
      stopViewerArriving,
      stopViewerConfig,
      timeFormat,
      transitOperators
    } = this.props;
    const {
      spin
    } = this.state; // construct a lookup table mapping pattern (e.g. 'ROUTE_ID-HEADSIGN') to
    // an array of stoptimes

    const stopTimesByPattern = (0, _viewer.getStopTimesByPattern)(stopData);

    const routeComparator = _coreUtils.default.route.makeRouteComparator(transitOperators);

    const patternHeadsignComparator = _coreUtils.default.route.makeStringValueComparator(pattern => pattern.pattern.headsign);

    const patternComparator = (patternA, patternB) => {
      // first sort by routes
      const routeCompareValue = routeComparator(patternA.route, patternB.route);
      if (routeCompareValue !== 0) return routeCompareValue; // if same route, sort by headsign

      return patternHeadsignComparator(patternA, patternB);
    };

    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", null, Object.values(stopTimesByPattern).sort(patternComparator).map(({
      id,
      pattern,
      route,
      times
    }) => {
      // Only add pattern if route info is returned by OTP.
      return (0, _viewer.routeIsValid)(route, (0, _viewer.getRouteIdForPattern)(pattern)) ? /*#__PURE__*/_react.default.createElement(_patternRow.default, {
        homeTimezone: homeTimezone,
        key: id,
        pattern: pattern,
        route: route,
        stopTimes: times,
        stopViewerArriving: stopViewerArriving,
        stopViewerConfig: stopViewerConfig,
        timeFormat: timeFormat
      }) : null;
    })), /*#__PURE__*/_react.default.createElement("div", {
      style: {
        marginTop: '20px'
      }
    }, /*#__PURE__*/_react.default.createElement("label", {
      style: {
        fontSize: 'small',
        fontWeight: 300
      }
    }, /*#__PURE__*/_react.default.createElement("input", {
      checked: this.props.autoRefreshStopTimes,
      name: "autoUpdate",
      onChange: this._onToggleAutoRefresh,
      type: "checkbox"
    }), ' ', "Auto-refresh arrivals?"), /*#__PURE__*/_react.default.createElement("button", {
      className: "link-button pull-right",
      onClick: this._refreshStopTimes,
      style: {
        fontSize: 'small'
      }
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      className: spin ? 'fa-spin' : '',
      type: "refresh"
    }), ' ', (0, _moment.default)(stopData.stopTimesLastUpdated).tz(getUserTimezone()).format(timeFormat))));
  }

}

var _default = LiveStopTimes;
exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=live-stop-times.js