"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RangeCalendar = void 0;
// @ts-nocheck
const react_1 = __importStar(require("react"));
const inlines_1 = require("inlines");
const varsUtilities_1 = require("../../varsUtilities");
const ContextState_1 = require("../../hooks/ContextState");
const Text_1 = require("../Text");
const utils_1 = require("../../utils");
const RangeCalendar = ({ selectedDay, selectedMonth, selectedYear, days, setSelectedDay, fromRangeCal, tillRangeCal, }) => {
    const [daysArr, setDaysArr] = (0, react_1.useState)([]);
    const [hoverDay, setHoverDay] = (0, react_1.useState)(null);
    const [hoverMonth, setHoverMonth] = (0, react_1.useState)(null);
    const [hoverYear, setHoverYear] = (0, react_1.useState)(null);
    const daysInMonth = (month, year) => {
        return new Date(year, month, 0).getDate();
    };
    const [fromValue] = (0, ContextState_1.useContextState)('fromValue');
    const [tillValue] = (0, ContextState_1.useContextState)('tillValue');
    const fromDateObj = new Date(+fromValue);
    const fromYear = fromDateObj.getFullYear();
    const fromMonth = fromDateObj.getMonth() + 1;
    const fromDay = fromDateObj.getDate();
    const tillDateObj = new Date(+tillValue);
    const tillYear = tillDateObj.getFullYear();
    const tillMonth = tillDateObj.getMonth() + 1;
    const tillDay = tillDateObj.getDate();
    // to determine the current day
    const dateObj = new Date();
    const currentMonth = dateObj.getMonth();
    const currentYear = dateObj.getFullYear();
    const presentDay = dateObj.getDate();
    const tempArr = [];
    // Calender layout offset
    (0, react_1.useEffect)(() => {
        tempArr.splice(0, tempArr.length);
        for (let i = 1; i <= daysInMonth(selectedMonth, selectedYear); i++) {
            tempArr.push({ day: i, month: selectedMonth, year: selectedYear });
        }
        // add some offset for the days layout
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Sun') {
            tempArr.unshift('x', 'x', 'x', 'x', 'x', 'x');
        }
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Sat') {
            tempArr.unshift('x', 'x', 'x', 'x', 'x');
        }
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Fri') {
            tempArr.unshift('x', 'x', 'x', 'x');
        }
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Thu') {
            tempArr.unshift('x', 'x', 'x');
        }
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Wed') {
            tempArr.unshift('x', 'x');
        }
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Tue') {
            tempArr.unshift('x');
        }
        if (days[new Date(`${selectedMonth} 1, ${selectedYear}`).getDay()] === 'Mon') {
            // tempArr.unshift()
        }
        setDaysArr(tempArr);
    }, [selectedMonth, selectedYear]);
    const checkIfRanged = (year, month, day) => {
        const fromTime = new Date(fromYear, fromMonth, fromDay).getTime();
        const tillTime = new Date(tillYear, tillMonth, tillDay).getTime();
        const checkTime = new Date(year, month, day).getTime();
        if (checkTime > fromTime && checkTime < tillTime) {
            return true;
        }
    };
    const checkIfFrom = (year, month, day) => {
        const fromTime = new Date(fromYear, fromMonth, fromDay).getTime();
        const checkTime = new Date(year, month, day).getTime();
        if (fromTime === checkTime) {
            return true;
        }
    };
    const checkIfTill = (year, month, day) => {
        const tillTime = new Date(tillYear, tillMonth, tillDay).getTime();
        const checkTime = new Date(year, month, day).getTime();
        if (checkTime === tillTime) {
            return true;
        }
    };
    const checkIfIsHoverDay = (year, month, day) => {
        const fromTime = new Date(fromYear, fromMonth, fromDay).getTime();
        const tillTime = new Date(tillYear, tillMonth, tillDay).getTime();
        const checkValTime = new Date(year, month, day).getTime();
        const hoverTime = new Date(hoverYear, hoverMonth, hoverDay).getTime();
        // if (checkValTime === (fromTime | tillTime)) return false
        if (checkValTime <= fromTime &&
            checkValTime >= hoverTime &&
            +hoverDay !== 0 &&
            tillTime > checkValTime &&
            fromRangeCal) {
            return true;
        }
        else if (checkValTime >= tillTime &&
            checkValTime <= hoverTime &&
            +hoverDay !== 0 &&
            fromTime < checkValTime &&
            tillRangeCal) {
            return true;
        }
    };
    const checkIfIsExact = (year, month, day) => {
        const fromTime = new Date(fromYear, fromMonth, fromDay).getTime();
        const tillTime = new Date(tillYear, tillMonth, tillDay).getTime();
        const checkValTime = new Date(year, month, day).getTime();
        const hoverTime = new Date(hoverYear, hoverMonth, hoverDay).getTime();
        // if (checkValTime === (fromTime | tillTime)) return false
        if (checkValTime === hoverTime &&
            +hoverDay !== 0 &&
            tillTime > checkValTime &&
            fromRangeCal) {
            return 'from';
        }
        else if (checkValTime === hoverTime &&
            +hoverDay !== 0 &&
            fromTime < checkValTime &&
            tillRangeCal) {
            return 'till';
        }
        return false;
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(inlines_1.styled.div, { style: {
                display: 'flex',
                textAlign: 'center',
                color: (0, varsUtilities_1.color)('content', 'default', 'secondary'),
                padding: '0px 20px',
                marginTop: 4,
                marginBottom: '-8px',
                justifyContent: 'center',
                userSelect: 'none',
                WebkitUserSelect: 'none',
                '& div': {
                    width: '26px',
                    textAlign: 'center',
                    margin: '4px',
                },
            } },
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "M"),
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "T"),
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "W"),
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "T"),
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "F"),
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "S"),
            react_1.default.createElement(Text_1.Text, { selectable: "none" }, "S")),
        react_1.default.createElement(inlines_1.styled.div, { style: { padding: '10px 20px' } }, daysArr.map((val, i) => val === 'x' ? (react_1.default.createElement(inlines_1.styled.div, { key: i, style: {
                width: 34,
                height: 26,
                // margin: '4px',
                marginTop: '4px',
                marginBottom: '4px',
                display: 'inline-flex',
                borderRadius: 4,
                alignItems: 'center',
                justifyContent: 'center',
                opacity: 0,
            } }, ".")) : (react_1.default.createElement(Text_1.Text, { onMouseEnter: () => {
                if (val.day !== null) {
                    setHoverDay(val.day);
                    setHoverMonth(val.month);
                    setHoverYear(val.year);
                }
            }, onMouseLeave: () => {
                setHoverDay(null);
                setHoverMonth(null);
                setHoverYear(null);
            }, style: {
                border: val.day === presentDay &&
                    +selectedMonth === currentMonth + 1 &&
                    +selectedYear === currentYear
                    ? `1px solid ${(0, varsUtilities_1.color)('border', 'brand', 'strong')}`
                    : '',
                background: val.day === +selectedDay
                    ? (0, varsUtilities_1.color)('action', 'primary', 'normal')
                    : val.day === fromDay &&
                        +selectedMonth === fromMonth &&
                        +selectedYear === fromYear
                        ? (0, varsUtilities_1.color)('action', 'primary', 'normal')
                        : val.day === tillDay &&
                            +selectedMonth === tillMonth &&
                            +selectedYear === tillYear
                            ? (0, varsUtilities_1.color)('action', 'primary', 'normal')
                            : checkIfRanged(val.year, val.month, val.day)
                                ? (0, varsUtilities_1.color)('action', 'primary', 'subtleNormal')
                                : checkIfIsHoverDay(val.year, val.month, val.day)
                                    ? (0, varsUtilities_1.color)('action', 'neutral', 'subtleHover')
                                    : '',
                color: val.day === +selectedDay
                    ? (0, varsUtilities_1.color)('content', 'inverted', 'primary')
                    : val.day === fromDay &&
                        +selectedMonth === fromMonth &&
                        +selectedYear === fromYear
                        ? (0, varsUtilities_1.color)('content', 'inverted', 'primary')
                        : val.day === tillDay &&
                            +selectedMonth === tillMonth &&
                            +selectedYear === tillYear
                            ? (0, varsUtilities_1.color)('content', 'inverted', 'primary')
                            : (0, varsUtilities_1.color)('content', 'default', 'primary'),
                boxSizing: 'border-box',
                width: 34,
                height: 26,
                marginTop: '4px',
                marginBottom: '4px',
                textAlign: 'center',
                display: 'inline-flex',
                alignItems: 'center',
                justifyContent: 'center',
                '&:hover': {
                    background: val.day === +selectedDay
                        ? (0, varsUtilities_1.color)('action', 'primary', 'hover')
                        : val.day === fromDay &&
                            +selectedMonth === fromMonth &&
                            +selectedYear === fromYear
                            ? (0, varsUtilities_1.color)('action', 'primary', 'hover')
                            : val.day === tillDay &&
                                +selectedMonth === tillMonth &&
                                +selectedYear === tillYear
                                ? (0, varsUtilities_1.color)('action', 'primary', 'hover')
                                : checkIfRanged(val.year, val.month, val.day)
                                    ? (0, varsUtilities_1.color)('action', 'primary', 'subtleSelected')
                                    : checkIfIsHoverDay(val.year, val.month, val.day)
                                        ? (0, varsUtilities_1.color)('action', 'primary', 'subtleSelected')
                                        : (0, varsUtilities_1.color)('action', 'neutral', 'subtleHover'),
                    borderRadius: fromValue === tillValue && fromDay === val.day
                        ? '4px'
                        : checkIfRanged(val.year, val.month, val.day)
                            ? 0
                            : checkIfIsExact(val.year, val.month, val.day) === 'from'
                                ? '4px 0 0 4px'
                                : checkIfIsExact(val.year, val.month, val.day) === 'till'
                                    ? '0 4px 4px 0'
                                    : val.day === fromDay &&
                                        +selectedMonth === fromMonth &&
                                        +selectedYear === fromYear
                                        ? undefined
                                        : val.day === tillDay &&
                                            +selectedMonth === tillMonth &&
                                            +selectedYear === tillYear
                                            ? undefined
                                            : '4px',
                },
                borderRadius: fromValue === tillValue && fromDay === val.day
                    ? '4px'
                    : checkIfIsHoverDay(val.year, val.month, val.day)
                        ? 0
                        : checkIfFrom(val.year, val.month, val.day)
                            ? '4px 0 0 4px'
                            : checkIfTill(val.year, val.month, val.day)
                                ? '0 4px 4px 0'
                                : val.day === presentDay &&
                                    +selectedMonth === currentMonth + 1 &&
                                    +selectedYear === currentYear
                                    ? checkIfRanged(val.year, val.month, val.day)
                                        ? 0
                                        : '4px'
                                    : 0,
                userSelect: 'none',
                WebkitUserSelect: 'none',
                [utils_1.BpTablet]: {
                    '&:hover': {
                        background: 'transparent',
                    },
                },
            }, key: i, onClick: () => {
                setSelectedDay(val.day < 10 ? `0${val.day}` : `${val.day}`);
            } }, val.day)))),
        react_1.default.createElement(inlines_1.styled.div, { style: {
                borderBottom: `1px solid ${(0, varsUtilities_1.color)('border', 'default', 'strong')}`,
            } })));
};
exports.RangeCalendar = RangeCalendar;
//# sourceMappingURL=RangeCalendar.js.map