"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dateToDos = dateToDos;
exports.parseDosDateTimeUTC = parseDosDateTimeUTC;
exports.resolveZipLastModifiedDateFromUnixSeconds = resolveZipLastModifiedDateFromUnixSeconds;
exports.resolveZipLastModifiedDateFromExtraField = resolveZipLastModifiedDateFromExtraField;
exports.buildZipTimestampExtraField = buildZipTimestampExtraField;
exports.dateToZipDos = dateToZipDos;
const zip_extra_1 = require("./zip-extra.js");
/**
 * DOS date/time helpers for ZIP files.
 */
/**
 * Convert Date to DOS time/date fields.
 *
 * Note: uses local time fields (getHours/getMinutes/getSeconds),
 * which matches common ZIP writer behavior.
 */
function dateToDos(date) {
    const dosTime = ((date.getHours() & 0x1f) << 11) |
        ((date.getMinutes() & 0x3f) << 5) |
        ((date.getSeconds() >> 1) & 0x1f);
    const dosDate = (((date.getFullYear() - 1980) & 0x7f) << 9) |
        (((date.getMonth() + 1) & 0x0f) << 5) |
        (date.getDate() & 0x1f);
    return [dosTime, dosDate];
}
/**
 * Parse DOS date/time to JS Date.
 */
function parseDosDateTimeUTC(date, time) {
    const day = date & 0x1f;
    const month = (date >> 5) & 0x0f;
    const year = ((date >> 9) & 0x7f) + 1980;
    const seconds = time ? (time & 0x1f) * 2 : 0;
    const minutes = time ? (time >> 5) & 0x3f : 0;
    const hours = time ? time >> 11 : 0;
    return new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds));
}
function resolveZipLastModifiedDateFromUnixSeconds(dosDate, dosTime, mtimeUnixSeconds) {
    if (mtimeUnixSeconds === undefined) {
        return parseDosDateTimeUTC(dosDate, dosTime);
    }
    return new Date(mtimeUnixSeconds * 1000);
}
function resolveZipLastModifiedDateFromExtraField(dosDate, dosTime, extraField) {
    const unixSeconds = (0, zip_extra_1.parseExtendedTimestampMtimeUnixSeconds)(extraField);
    return resolveZipLastModifiedDateFromUnixSeconds(dosDate, dosTime, unixSeconds);
}
function buildZipTimestampExtraField(modTime, mode) {
    return mode === "dos+utc" ? (0, zip_extra_1.buildExtendedTimestampExtraFieldFromDate)(modTime) : new Uint8Array(0);
}
function dateToZipDos(modTime) {
    const [dosTime, dosDate] = dateToDos(modTime);
    return { dosTime, dosDate };
}
