import { Type } from "../type.ts";
const YAML_DATE_REGEXP = new RegExp("^([0-9][0-9][0-9][0-9])" +
    "-([0-9][0-9])" +
    "-([0-9][0-9])$");
const YAML_TIMESTAMP_REGEXP = new RegExp("^([0-9][0-9][0-9][0-9])" +
    "-([0-9][0-9]?)" +
    "-([0-9][0-9]?)" +
    "(?:[Tt]|[ \\t]+)" +
    "([0-9][0-9]?)" +
    ":([0-9][0-9])" +
    ":([0-9][0-9])" +
    "(?:\\.([0-9]*))?" +
    "(?:[ \\t]*(Z|([-+])([0-9][0-9]?)" +
    "(?::([0-9][0-9]))?))?$");
function resolveYamlTimestamp(data) {
    if (data === null)
        return false;
    if (YAML_DATE_REGEXP.exec(data) !== null)
        return true;
    if (YAML_TIMESTAMP_REGEXP.exec(data) !== null)
        return true;
    return false;
}
function constructYamlTimestamp(data) {
    let match = YAML_DATE_REGEXP.exec(data);
    if (match === null)
        match = YAML_TIMESTAMP_REGEXP.exec(data);
    if (match === null)
        throw new Error("Date resolve error");
    const year = +match[1];
    const month = +match[2] - 1;
    const day = +match[3];
    if (!match[4]) {
        return new Date(Date.UTC(year, month, day));
    }
    const hour = +match[4];
    const minute = +match[5];
    const second = +match[6];
    let fraction = 0;
    if (match[7]) {
        let partFraction = match[7].slice(0, 3);
        while (partFraction.length < 3) {
            partFraction += "0";
        }
        fraction = +partFraction;
    }
    let delta = null;
    if (match[9]) {
        const tzHour = +match[10];
        const tzMinute = +(match[11] || 0);
        delta = (tzHour * 60 + tzMinute) * 60000;
        if (match[9] === "-")
            delta = -delta;
    }
    const date = new Date(Date.UTC(year, month, day, hour, minute, second, fraction));
    if (delta)
        date.setTime(date.getTime() - delta);
    return date;
}
function representYamlTimestamp(date) {
    return date.toISOString();
}
export const timestamp = new Type("tag:yaml.org,2002:timestamp", {
    construct: constructYamlTimestamp,
    instanceOf: Date,
    kind: "scalar",
    represent: representYamlTimestamp,
    resolve: resolveYamlTimestamp,
});
//# sourceMappingURL=data:application/json;base64,[encoded data, 7040 characters]