"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatePickerDayStatus = void 0;
exports.default = DateRangePickerDay;
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const DateRangePickerContext_1 = require("./DateRangePickerContext");
const Text_1 = __importStar(require("../Text"));
const BookingWizard_1 = require("../BookingWizard/BookingWizard");
const DateRangePicker_module_scss_1 = __importDefault(require("./DateRangePicker.module.scss"));
var DatePickerDayStatus;
(function (DatePickerDayStatus) {
    DatePickerDayStatus[DatePickerDayStatus["Inactive"] = 0] = "Inactive";
    DatePickerDayStatus["Selecting"] = "--selecting";
    DatePickerDayStatus["Loading"] = "--loading";
    DatePickerDayStatus["Selected"] = "--selected";
})(DatePickerDayStatus || (exports.DatePickerDayStatus = DatePickerDayStatus = {}));
function DateRangePickerDay(props) {
    var _a, _b;
    const datePickerContext = (0, react_1.useContext)(DateRangePickerContext_1.DateRangePickerContext);
    const [isHovering, setIsHovering] = (0, react_1.useState)(false);
    const [isSelected, setIsSelected] = (0, react_1.useState)(false);
    const [isSelecting, setIsSelecting] = (0, react_1.useState)(false);
    const [isFirst, setIsFirst] = (0, react_1.useState)(false);
    const [isLast, setIsLast] = (0, react_1.useState)(false);
    const isCurrent = props.day && ((_a = datePickerContext.today) === null || _a === void 0 ? void 0 : _a.isSame(props.day, 'date'));
    const isPast = (props.unix < datePickerContext.todayInUnix && !isCurrent) || (datePickerContext.fixedStartDate && ((_b = datePickerContext.startDate) === null || _b === void 0 ? void 0 : _b.isAfter(props.day)));
    (0, react_1.useEffect)(() => {
        var _a;
        if (datePickerContext.hoveredMonth) {
            if (props.unix && ((_a = datePickerContext.startDate) === null || _a === void 0 ? void 0 : _a.isSameOrBefore(props.day, 'date')) && datePickerContext.hoveredMonth.isSameOrAfter(props.day, 'date')) {
                setIsSelecting(true);
                if (datePickerContext.hoveredMonth.isSame(props.day, 'date') || (datePickerContext.endDate && datePickerContext.endDate.isSame(props.day))) {
                    setIsLast(true);
                }
                else {
                    setIsLast(false);
                }
                return;
            }
        }
        setIsSelecting(false);
        setIsLast(false);
    }, [datePickerContext.hoveredMonth, props.day, datePickerContext.endDate, props.unix, datePickerContext.startDate]);
    (0, react_1.useEffect)(() => {
        var _a, _b, _c;
        if (props.unix && datePickerContext.startDate) {
            if (((_a = datePickerContext.startDate) === null || _a === void 0 ? void 0 : _a.isBefore(props.day, 'date')) && ((_b = datePickerContext.endDate) === null || _b === void 0 ? void 0 : _b.isSameOrAfter(props.day, 'date'))) {
                setIsSelected(true);
                if ((_c = datePickerContext.endDate) === null || _c === void 0 ? void 0 : _c.isSame(props.day, 'date')) {
                    setIsLast(true);
                }
                return;
            }
            if (datePickerContext.startDate.isSame(props.day, 'date')) {
                setIsFirst(true);
                setIsSelected(true);
                return;
            }
        }
        setIsSelected(false);
        setIsFirst(false);
        setIsLast(false);
    }, [datePickerContext.startDate, datePickerContext.endDate, props.day]);
    const memo = (0, react_1.useMemo)(() => {
        const onHoverIn = (e) => {
            datePickerContext.onHoverOver(props.day);
            setIsHovering(true);
        };
        const onHoverOut = (e) => {
            setIsHovering(false);
        };
        const onClick = (e) => {
            datePickerContext.onDayClicked(props.day);
        };
        const isSelectable = () => {
            return props.day && props.selectable && !isPast;
        };
        const isLastHovered = () => {
            return (props.unix &&
                datePickerContext.startDate &&
                datePickerContext.hoveredMonth &&
                datePickerContext.startDate.isBefore(props.day, 'date') &&
                datePickerContext.hoveredMonth.isSame(props.day, 'date'));
        };
        const isDisabled = () => datePickerContext.fixedStartDate && props.day && datePickerContext.startDate && props.day.isBefore(datePickerContext.startDate);
        const classes = (0, classnames_1.default)(DateRangePicker_module_scss_1.default[`roomstay-calendar-month--item`], {
            [DateRangePicker_module_scss_1.default['--selectable']]: isSelectable() && !isDisabled(),
            [DateRangePicker_module_scss_1.default['--hovered']]: isHovering && isSelectable(),
            [DateRangePicker_module_scss_1.default['--past']]: !isSelectable(),
            [DateRangePicker_module_scss_1.default['--current']]: isCurrent,
            [DateRangePicker_module_scss_1.default['--first']]: isFirst,
            [DateRangePicker_module_scss_1.default['--last']]: isLast,
            [DateRangePicker_module_scss_1.default['--selected']]: isSelected,
            [DateRangePicker_module_scss_1.default['--selecting']]: isSelecting,
            [DateRangePicker_module_scss_1.default['--last-hovered']]: isLastHovered(),
            [DateRangePicker_module_scss_1.default['--finish']]: (isFirst || isLast) && datePickerContext.startDate && datePickerContext.endDate,
        });
        const events = {};
        events.onMouseEnter = onHoverIn;
        events.onMouseLeave = onHoverOut;
        if (isSelectable() && !isDisabled())
            events.onClick = onClick;
        return (react_1.default.createElement("div", Object.assign({ className: classes }, events),
            react_1.default.createElement(Text_1.default, { type: datePickerContext.theme === BookingWizard_1.EBookingWizardTheme.Specific ? Text_1.TextType.Small : undefined, color: props.color }, props.text)));
    }, [isFirst, isLast, isSelecting, isSelected, isHovering, props.unix, datePickerContext.hoveredMonth, datePickerContext.startDate, datePickerContext.endDate]);
    return memo;
}
//# sourceMappingURL=DateRangePickerDay.js.map