'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

require('./style.css');

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

var getDayOfWeek = function getDayOfWeek() {
  var d = new Date();
  var weekday = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

  return weekday[d.getDay()];
};
var currentDay = getDayOfWeek();

var OperationalHours = function OperationalHours(props) {
  var listing = props.listKey ? '--listing' : '';
  return _react2.default.createElement(
    'div',
    { className: 'ma__operational-hours' },
    _react2.default.createElement(
      'table',
      { className: 'ma__operational-hours-table' + listing },
      _react2.default.createElement(
        'tbody',
        null,
        props.hours && Object.entries(props.hours).map(function (_ref) {
          var key = _ref[0],
              value = _ref[1];

          var active = props.showActive && key === props.currentDay ? '--active' : '';
          var rowKey = props.listKey ? props.listKey + '-row-' + key : null;
          var status = typeof value.status !== 'undefined' && value.status !== null ? value.status : true;
          var time = '';
          var dateFormat = new Intl.DateTimeFormat('en-US', {
            hour12: true,
            hour: 'numeric',
            minute: 'numeric'
          });
          if (status === false) {
            time = 'Closed';
          } else {
            time = dateFormat.format(value.start) + ' - ' + dateFormat.format(value.end);
          }
          return _react2.default.createElement(
            'tr',
            {
              key: rowKey,
              className: 'ma__operational-hours-row' + active
            },
            _react2.default.createElement(
              'td',
              { className: 'ma__operational-hours-day' },
              key
            ),
            _react2.default.createElement(
              'td',
              { className: 'ma__operational-hours-time' },
              time
            )
          );
        })
      )
    )
  );
};

OperationalHours.propTypes = process.env.NODE_ENV !== "production" ? {
  /** An object that is keyed by the days of the week. Each day
      contains the following:
      status: represents an open or closed day of business for the week. If true, the business is open.
      start: The time the business opens.
      end: The time the business closes. */
  hours: _propTypes2.default.shape({
    monday: _propTypes2.default.shape({
      status: _propTypes2.default.bool,
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    }),
    tuesday: _propTypes2.default.shape({
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    }),
    wednesday: _propTypes2.default.shape({
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    }),
    thursday: _propTypes2.default.shape({
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    }),
    friday: _propTypes2.default.shape({
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    }),
    saturday: _propTypes2.default.shape({
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    }),
    sunday: _propTypes2.default.shape({
      start: _propTypes2.default.instanceOf(Date),
      end: _propTypes2.default.instanceOf(Date)
    })
  }).isRequired,
  /** Whether to show if currently active or not. */
  showActive: _propTypes2.default.bool,
  /** A unique string used for generating the display of days for week. */
  listKey: _propTypes2.default.string.isRequired,
  /** Current day for the week that gets highlighted. */
  currentDay: _propTypes2.default.string
} : {};

OperationalHours.defaultProps = {
  showActive: true,
  currentDay: currentDay
};

exports.default = OperationalHours;
module.exports = exports['default'];