"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 _velocityReact = require("velocity-react");

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

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

var _realtimeStatusLabel = _interopRequireDefault(require("./realtime-status-label"));

var _stopTimeCell = _interopRequireDefault(require("./stop-time-cell"));

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

/**
 * Represents a single pattern row for displaying arrival times in the stop
 * viewer.
 */
class PatternRow extends _react.Component {
  constructor() {
    super();

    _defineProperty(this, "_toggleExpandedView", () => {
      this.setState({
        expanded: !this.state.expanded
      });
    });

    this.state = {
      expanded: false
    };
  }

  render() {
    const {
      homeTimezone,
      pattern,
      route,
      stopTimes,
      stopViewerArriving,
      stopViewerConfig,
      timeFormat
    } = this.props; // sort stop times by next departure

    let sortedStopTimes = [];
    const hasStopTimes = stopTimes && stopTimes.length > 0;

    if (hasStopTimes) {
      sortedStopTimes = stopTimes.concat().sort(_viewer.stopTimeComparator) // We request only x departures per pattern, but the patterns are merged
      // according to shared headsigns, so we need to slice the stop times
      // here as well to ensure only x times are shown per route/headsign combo.
      // This is applied after the sort, so we're keeping the soonest departures.
      .slice(0, stopViewerConfig.numberOfDepartures);
    } else {
      // Do not render pattern row if it has no stop times.
      return null;
    }

    const routeName = route.shortName ? route.shortName : route.longName;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: "route-row",
      role: "table"
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "header",
      role: "row"
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "route-name",
      role: "columnheader"
    }, /*#__PURE__*/_react.default.createElement("b", null, routeName), " To ", pattern.headsign), hasStopTimes && /*#__PURE__*/_react.default.createElement("div", {
      className: "next-trip-preview",
      role: "columnheader"
    }, /*#__PURE__*/_react.default.createElement(_stopTimeCell.default, {
      homeTimezone: homeTimezone,
      soonText: stopViewerArriving,
      stopTime: sortedStopTimes[0],
      timeFormat: timeFormat
    })), /*#__PURE__*/_react.default.createElement("div", {
      className: "expansion-button-container"
    }, /*#__PURE__*/_react.default.createElement("button", {
      "aria-controls": `route-${routeName}`,
      "aria-expanded": this.state.expanded,
      "aria-label": `${this.state.expanded ? 'Collapse' : 'Expand'} all departures for Route ${routeName}`,
      className: "expansion-button",
      onClick: this._toggleExpandedView
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      type: `chevron-${this.state.expanded ? 'up' : 'down'}`
    })))), /*#__PURE__*/_react.default.createElement(_velocityReact.VelocityTransitionGroup, {
      enter: {
        animation: 'slideDown'
      },
      leave: {
        animation: 'slideUp'
      }
    }, this.state.expanded && /*#__PURE__*/_react.default.createElement("div", {
      id: `route-${routeName}`
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "trip-table",
      role: "table"
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "header",
      role: "row"
    }, /*#__PURE__*/_react.default.createElement("div", {
      "aria-label": "route",
      className: "cell",
      role: "columnheader"
    }), /*#__PURE__*/_react.default.createElement("div", {
      "aria-sort": "descending",
      className: "cell time-column",
      role: "columnheader"
    }, "DEPARTURE"), /*#__PURE__*/_react.default.createElement("div", {
      className: "cell status-column",
      role: "columnheader"
    }, "STATUS")), hasStopTimes && sortedStopTimes.map((stopTime, i) => {
      const {
        departureDelay: delay,
        realtimeState
      } = stopTime;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: "trip-row",
        key: i,
        role: "row",
        style: {
          display: 'table-row',
          fontSize: 14,
          marginTop: 6
        }
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "cell",
        role: "cell"
      }, "To ", stopTime.headsign), /*#__PURE__*/_react.default.createElement("div", {
        className: "cell time-column",
        role: "cell"
      }, /*#__PURE__*/_react.default.createElement(_stopTimeCell.default, {
        homeTimezone: homeTimezone,
        soonText: stopViewerArriving,
        stopTime: stopTime,
        timeFormat: timeFormat
      })), /*#__PURE__*/_react.default.createElement("div", {
        className: "cell status-column",
        role: "cell"
      }, /*#__PURE__*/_react.default.createElement(_realtimeStatusLabel.default, {
        className: "status-label",
        delay: delay,
        isRealtime: realtimeState === 'UPDATED',
        withBackground: true
      })));
    })))));
  }

}

exports.default = PatternRow;
module.exports = exports.default;

//# sourceMappingURL=pattern-row.js