"use strict";

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

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

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

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

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _loading = _interopRequireDefault(require("../narrative/loading"));

var _constants = require("../../util/constants");

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

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 {
  formatSecondsAfterMidnight
} = _coreUtils.default.time; // Styles for the schedule table and its contents.

const StyledTable = _styledComponents.default.table`
  border-spacing: collapse;
  height: 100%;
  width: 100%;
  th {
    background-color: #fff;
    box-shadow: 0 1px 0px 0px #ccc;
    font-size: 75%;
    position: sticky;
    top: 0px;
  }
  tr > * {
    border-bottom: 1px solid #ccc;
    padding: 2px 0 2px 10px;
    vertical-align: top;
  }
  td:first-child, th:first-child {
    padding-left: 0;
  }
`; // Limit the width of the block cell if agency uses very long block IDs,
// otherwise the table contents overflows the width of the side pane.
// FIXME: Determine a good way to display long block ids.

const BlockCell = _styledComponents.default.td`
  max-width: 100px;
  overflow-x: hidden;
  text-overflow: ellipsis;
`;
const RouteCell = _styledComponents.default.td`
  font-weight: bold;
`;
const TimeCell = _styledComponents.default.td`
  font-weight: bold;
  text-align: right;
  white-space: nowrap;
`;
/**
 * Table showing scheduled departure times for the specified stop organized
 * chronologically.
 */

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

    _defineProperty(this, "targetDepartureRef", /*#__PURE__*/(0, _react.createRef)());

    _defineProperty(this, "_scrollToFirstDeparture", () => {
      const {
        current
      } = this.targetDepartureRef;

      if (current) {
        current.scrollIntoView({
          behavior: 'smooth',
          block: 'start'
        });
      }
    });
  }

  componentDidMount() {
    this._scrollToFirstDeparture();
  }

  componentDidUpdate() {
    // Should only happen if user changes date and a new stopData is passed.
    this._scrollToFirstDeparture();
  }

  render() {
    const {
      date,
      showBlockIds,
      stopData,
      timeFormat
    } = this.props; // Show loading spinner if times are still being fetched.

    if (stopData.fetchStatus === _constants.FETCH_STATUS.FETCHING) {
      return /*#__PURE__*/_react.default.createElement(_loading.default, {
        small: true
      });
    }

    const mergedStopTimes = (0, _viewer.mergeAndSortStopTimes)(stopData);
    const today = (0, _moment.default)().startOf('day').format('YYYY-MM-DD'); // Find the next stop time that is departing.
    // We will scroll to that stop time entry (if showing schedules for today).

    const shouldHighlightFirstDeparture = mergedStopTimes.length && date === today;
    const highlightedStopTime = shouldHighlightFirstDeparture ? (0, _viewer.getFirstDepartureFromNow)(mergedStopTimes) : null;
    return /*#__PURE__*/_react.default.createElement(StyledTable, null, /*#__PURE__*/_react.default.createElement("thead", null, /*#__PURE__*/_react.default.createElement("tr", null, showBlockIds && /*#__PURE__*/_react.default.createElement("th", null, "Block"), /*#__PURE__*/_react.default.createElement("th", null, "Route"), /*#__PURE__*/_react.default.createElement("th", null, "To"), /*#__PURE__*/_react.default.createElement("th", null, "Departure"))), /*#__PURE__*/_react.default.createElement("tbody", null, mergedStopTimes.map((stopTime, index) => {
      const {
        blockId,
        headsign,
        route
      } = stopTime; // Highlight if this row is the imminent departure and schedule is shown for today.

      const highlightRow = stopTime === highlightedStopTime;
      const className = highlightRow ? 'highlighted-item' : null; // This is a bit of a hack to account for the sticky table
      // header interfering with the scrollIntoView. If the next stop time
      // is the imminent departure, we'll set the scrollTo to this row (the
      // stop time prior), which effectively applies an offset for the
      // scroll. If next row does not exist, default to this row.

      const nextStopTime = mergedStopTimes[index + 1];
      const scrollToRow = nextStopTime ? nextStopTime === highlightedStopTime : highlightRow;
      const routeName = route.shortName ? route.shortName : route.longName;
      const formattedTime = formatSecondsAfterMidnight(stopTime.scheduledDeparture, timeFormat); // Add ref to scroll to the first stop time departing from now.

      const refProp = scrollToRow ? this.targetDepartureRef : null;
      return /*#__PURE__*/_react.default.createElement("tr", {
        className: className,
        key: index,
        ref: refProp
      }, showBlockIds && /*#__PURE__*/_react.default.createElement(BlockCell, {
        title: blockId
      }, blockId), /*#__PURE__*/_react.default.createElement(RouteCell, null, routeName), /*#__PURE__*/_react.default.createElement("td", null, headsign), /*#__PURE__*/_react.default.createElement(TimeCell, null, formattedTime));
    })));
  }

}

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

//# sourceMappingURL=stop-schedule-table.js