"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.getStateForValue = getStateForValue;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _comparison = require("../../../lib/date/comparison");
var _constants = require("../../../lib/date/constants");








var defaultContextOptions = {
  currentStart: '',
  currentEnd: '',
  minDate: _constants.MIN_FULLDATE,
  maxDate: _constants.MAX_FULLDATE
};

function getStateForValue(
currentFocus,
value,
contextOptions)
{if (value === void 0) {value = '';}if (contextOptions === void 0) {contextOptions = {};}
  var _defaultContextOption = (0, _extends2.default)({}, defaultContextOptions, contextOptions),currentStart = _defaultContextOption.currentStart,currentEnd = _defaultContextOption.currentEnd,minDate = _defaultContextOption.minDate,maxDate = _defaultContextOption.maxDate;

  var start = currentStart;
  var end = currentEnd;
  var focus = currentFocus;
  var isOpen = true;

  if (minDate && (0, _comparison.isLess)(value, minDate) || maxDate && (0, _comparison.isGreater)(value, maxDate)) {
    return {
      start: start,
      end: end,
      focus: focus,
      isOpen: isOpen
    };
  }

  var handleInitialPeriod = function handleInitialPeriod(date) {
    if (currentFocus === 'start') {
      start = date;
      focus = 'end';
    } else {
      end = date;
      focus = 'start';
    }
  };

  var handlePartialPeriod = function handlePartialPeriod(date) {
    if (currentFocus === 'start') {
      if (currentStart) {
        start = date;
        focus = 'end';
        return;
      }

      if (currentEnd && (0, _comparison.isGreater)(date, currentEnd)) {
        start = date;
        end = '';
        focus = 'end';
        return;
      }

      start = date;
      isOpen = false;
    } else if (currentFocus === 'end') {
      if (currentEnd) {
        end = date;
        focus = 'start';
        return;
      }

      if (currentStart && (0, _comparison.isLess)(date, currentStart)) {
        start = date;
        end = '';
        focus = 'end';
        return;
      }

      end = date;
      isOpen = false;
    }
  };

  var handleFullPeriod = function handleFullPeriod(date) {
    if (currentFocus === 'start') {
      if (currentEnd && (0, _comparison.isLessOrEqual)(date, currentEnd)) {
        start = date;
        isOpen = false;
      } else {
        start = date;
        end = '';
        focus = 'end';
      }
    } else if (currentFocus === 'end') {
      if (currentStart && (0, _comparison.isGreaterOrEqual)(date, currentStart)) {
        end = date;
        isOpen = false;
      } else {
        start = date;
        end = '';
        focus = 'end';
      }
    }
  };

  if (!currentStart && !currentEnd) {
    handleInitialPeriod(value);
  } else if (currentStart && !currentEnd || !currentStart && currentEnd) {
    handlePartialPeriod(value);
  } else {
    handleFullPeriod(value);
  }

  return {
    start: start,
    end: end,
    focus: focus,
    isOpen: isOpen
  };
}