var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { isLess, isGreater, isLessOrEqual, isGreaterOrEqual } from '../../../lib/date/comparison.js';
import { MIN_FULLDATE, MAX_FULLDATE } from '../../../lib/date/constants.js';
var defaultContextOptions = {
    currentStart: '',
    currentEnd: '',
    minDate: MIN_FULLDATE,
    maxDate: MAX_FULLDATE,
};
export function getStateForValue(currentFocus, value, contextOptions) {
    if (value === void 0) { value = ''; }
    if (contextOptions === void 0) { contextOptions = {}; }
    var _a = __assign(__assign({}, defaultContextOptions), contextOptions), currentStart = _a.currentStart, currentEnd = _a.currentEnd, minDate = _a.minDate, maxDate = _a.maxDate;
    var start = currentStart;
    var end = currentEnd;
    var focus = currentFocus;
    var isOpen = true;
    if ((minDate && isLess(value, minDate)) || (maxDate && isGreater(value, maxDate))) {
        return {
            start: start,
            end: end,
            focus: focus,
            isOpen: isOpen,
        };
    }
    var handleInitialPeriod = function (date) {
        if (currentFocus === 'start') {
            start = date;
            focus = 'end';
        }
        else {
            end = date;
            focus = 'start';
        }
    };
    var handlePartialPeriod = function (date) {
        if (currentFocus === 'start') {
            if (currentStart) {
                start = date;
                focus = 'end';
                return;
            }
            if (currentEnd && 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 && isLess(date, currentStart)) {
                start = date;
                end = '';
                focus = 'end';
                return;
            }
            end = date;
            isOpen = false;
        }
    };
    var handleFullPeriod = function (date) {
        if (currentFocus === 'start') {
            if (currentEnd && isLessOrEqual(date, currentEnd)) {
                start = date;
                isOpen = false;
            }
            else {
                start = date;
                end = '';
                focus = 'end';
            }
        }
        else if (currentFocus === 'end') {
            if (currentStart && 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,
    };
}
//# sourceMappingURL=getStateForValue.js.map