'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _themeGet = require('../../../extensions/themeGet');

var _themeGet2 = _interopRequireDefault(_themeGet);

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

var Bar = _styledComponents2.default.div.withConfig({
  displayName: 'TimeScrubberTimeline__Bar'
})(['display:flex;flex-direction:row;position:absolute;bottom:0;top:0;user-select:none;transition:', ';& > *{margin-top:auto;margin-right:', 'px;}'], function (props) {
  return props.dragging ? '0' : '0.2s ease all';
}, function (props) {
  return props.tickSpacing - 1;
});

var Tick = _styledComponents2.default.div.withConfig({
  displayName: 'TimeScrubberTimeline__Tick'
})(['border-left:1px solid ', ';height:', ';position:relative;&::after{content:\'', '\';font-size:12px;position:absolute;bottom:70%;transform:translateX(-50%);white-space:pre;opacity:', ';}'], (0, _themeGet2.default)('colors.border.medium'), function (props) {
  return props.primary ? '10px' : '5px';
}, function (props) {
  return props.label;
}, function (props) {
  return props.enabled ? 1 : 0.5;
});

var getTickLabel = function getTickLabel(idx) {
  if (idx % 4 !== 0) {
    return '';
  }

  var hour = idx / 4;
  var pm = hour > 11 && hour < 24;
  var displayHour = hour % 12 === 0 ? 12 : hour % 12;
  return displayHour + ' ' + (pm ? 'PM' : 'AM');
};

var rangeContains = function rangeContains(range, value) {
  return value >= range[0] && value <= range[1];
};

/**
 * Draws a timeline with times, tickmarks
 */

var Timeline = function (_React$PureComponent) {
  (0, _inherits3.default)(Timeline, _React$PureComponent);

  function Timeline() {
    (0, _classCallCheck3.default)(this, Timeline);
    return (0, _possibleConstructorReturn3.default)(this, (Timeline.__proto__ || (0, _getPrototypeOf2.default)(Timeline)).apply(this, arguments));
  }

  (0, _createClass3.default)(Timeline, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          tickSpacing = _props.tickSpacing,
          allowedRange = _props.allowedRange,
          rest = (0, _objectWithoutProperties3.default)(_props, ['tickSpacing', 'allowedRange']);

      return _react2.default.createElement(
        Bar,
        (0, _extends3.default)({ tickSpacing: tickSpacing }, rest),
        new Array(97).fill(null).map(function (_, idx) {
          return _react2.default.createElement(Tick, {
            key: idx,
            primary: idx % 4 === 0,
            label: getTickLabel(idx),
            enabled: idx !== 96 && rangeContains(allowedRange, idx / 4.0)
          });
        })
      );
    }
  }]);
  return Timeline;
}(_react2.default.PureComponent);

Timeline.defaultProps = {
  tickSpacing: 11,
  allowedRange: [0, 23.75]
};
exports.default = Timeline;