"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.NewTimeInput = void 0;
const react_1 = __importStar(require("react"));
const varsUtilities_1 = require("../../varsUtilities");
const inlines_1 = require("inlines");
const Input_1 = require("../Input");
const NewTimeInput = ({ setTimeString, timeString, style }) => {
    const [, setValidTimeInput] = (0, react_1.useState)(false);
    const [isFocus, setIsFocus] = (0, react_1.useState)(false);
    // so it does not crash
    const [tempTime, setTempTime] = (0, react_1.useState)(timeString);
    const timeRegex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;
    // alleen hier echte tijd zetten als het kan pas
    (0, react_1.useEffect)(() => {
        if (tempTime?.length === 6) {
            setTimeString(timeString?.slice(0, -1));
        }
        //  check if regex matches
        if (timeRegex.test(tempTime) && tempTime.length === 5) {
            setValidTimeInput(true);
            setTimeString(tempTime);
        }
        else {
            setValidTimeInput(false);
        }
    }, [tempTime]);
    const timeHandler = (e) => {
        // placeholder = 'hh:mm'
        if (e.length === 1 && e > 2) {
            e = '0' + e;
        }
        if (e.length === 2) {
            setTempTime(e + ':');
        }
        else {
            setTempTime(e);
        }
        if (e.length === 3 && e.split('').pop() !== ':') {
            let temp = e.split('');
            temp.splice(2, 0, ':');
            temp = temp.join('');
            e = temp;
            setTempTime(temp);
        }
        if (e.length === 4) {
            if (+e.slice(-1) > 5) {
                let temp = e.split('');
                temp.splice(3, 0, '0');
                temp = temp.join('');
                setTempTime(temp);
            }
        }
    };
    const keyPressHandler = (e) => {
        if (isNaN(e.key)) {
            e.preventDefault();
        }
    };
    return (react_1.default.createElement(inlines_1.styled.div, { style: {
            position: 'relative',
            backgroundColor: isFocus
                ? (0, varsUtilities_1.color)('action', 'neutral', 'subtleNormal')
                : (0, varsUtilities_1.color)('action', 'neutral', 'subtleActive'),
            borderRadius: 8,
            ...style,
        }, onFocus: () => {
            setIsFocus(true);
        }, onBlur: () => {
            setIsFocus(false);
        } },
        react_1.default.createElement(Input_1.Input, { value: tempTime, type: "text", 
            // onKeyPress={keyPressHandler}
            onChange: timeHandler, placeholder: "00:00" }),
        isFocus && (react_1.default.createElement("div", { style: {
                //    border: `2px solid ${genColor('accent')}`,
                position: 'absolute',
                top: 0,
                left: 0,
                right: 0,
                bottom: 0,
                borderRadius: 4,
            } }))));
};
exports.NewTimeInput = NewTimeInput;
//# sourceMappingURL=NewTimeInput.js.map