"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.TimeInput = void 0;
const react_1 = __importStar(require("react"));
const styled_components_1 = require("styled-components");
const SimpleElements_1 = require("../SimpleElements");
const timeInputClass = styled_components_1.css `
  width: 5em;
  font-feature-settings: 'tnum';
  font-variant-numeric: tabular-nums;
`;
const regexp = /^\d{0,2}?:?\d{0,2}$/;
const isValidHour = (h) => Number.isInteger(h) && h >= 0 && h < 24;
const isValidMinutes = (m) => (Number.isInteger(m) && m >= 0 && m < 60) || Number.isNaN(m);
class TimeInput extends react_1.Component {
    constructor() {
        super(...arguments);
        this.state = {
            value: this.props.value || '00:00',
        };
        this.lastVal = '';
    }
    isValid(val) {
        if (!regexp.test(val))
            return false;
        const [hh, mm] = val.split(':');
        const hours = Number(hh);
        const minutes = Number(mm);
        if (!isValidHour(hours) || !isValidMinutes(minutes)) {
            return false;
        }
        // first mm digit must be <= 5
        if (minutes < 10 && Number(mm[0]) > 5)
            return false;
        return true;
    }
    onChange(value) {
        if (value === this.state.value)
            return;
        if (this.isValid(value)) {
            if (value.length === 2 &&
                this.lastVal.length !== 3 &&
                !value.includes(':')) {
                value += ':';
            }
            if (value.length === 2 && this.lastVal.length === 3) {
                value = value.slice(0, 1);
            }
            if (value.length > 5) {
                return;
            }
            this.lastVal = value;
            this.setState({ value });
            if (value.length === 5) {
                const { onChange } = this.props;
                if (onChange)
                    onChange(value);
            }
        }
    }
    render() {
        return (react_1.default.createElement(SimpleElements_1.SimpleInput, { css: timeInputClass, type: "tel", value: this.state.value, placeholder: this.props.placeholder, onChange: (e) => this.onChange(e.target.value) }));
    }
}
exports.TimeInput = TimeInput;
//# sourceMappingURL=index.js.map