"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get VALIDATOR_TYPE () {
        return VALIDATOR_TYPE;
    },
    get timeValidator () {
        return timeValidator;
    }
});
const _moment = /*#__PURE__*/ _interop_require_default(require("moment"));
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
// Formats which are correctly parsed to time (supported by momentjs)
const STRICT_FORMATS = [
    'YYYY-MM-DDTHH:mm:ss.SSSZ',
    'X',
    'x' // Unix ms timestamp
];
const VALIDATOR_TYPE = 'time';
function timeValidator(value, callback) {
    const timeFormat = this.timeFormat || 'h:mm:ss a';
    let valid = true;
    let valueToValidate = value;
    if (valueToValidate === null) {
        valueToValidate = '';
    }
    valueToValidate = /^\d{3,}$/.test(valueToValidate) ? parseInt(valueToValidate, 10) : valueToValidate;
    const twoDigitValue = /^\d{1,2}$/.test(valueToValidate);
    if (twoDigitValue) {
        valueToValidate += ':00';
    }
    const date = (0, _moment.default)(valueToValidate, STRICT_FORMATS, true).isValid() ? (0, _moment.default)(valueToValidate) : (0, _moment.default)(valueToValidate, timeFormat);
    let isValidTime = date.isValid();
    // is it in the specified format
    let isValidFormat = (0, _moment.default)(valueToValidate, timeFormat, true).isValid() && !twoDigitValue;
    if (this.allowEmpty && valueToValidate === '') {
        isValidTime = true;
        isValidFormat = true;
    }
    if (!isValidTime) {
        valid = false;
    }
    if (!isValidTime && isValidFormat) {
        valid = true;
    }
    if (isValidTime && !isValidFormat) {
        if (this.correctFormat === true) {
            const correctedValue = date.format(timeFormat);
            this.instance.setDataAtCell(this.visualRow, this.visualCol, correctedValue, 'timeValidator');
            valid = true;
        } else {
            valid = false;
        }
    }
    callback(valid);
}
timeValidator.VALIDATOR_TYPE = VALIDATOR_TYPE;
