import path from "node:path";
const FALLBACK_APP_NAME = "app";
const INVALID_FILENAME_CHARACTERS = /[<>:"/\\|?*]/g;
function stripControlCharacters(value) {
    return Array.from(value)
        .filter((char) => {
        const codePoint = char.codePointAt(0);
        return codePoint !== undefined && codePoint >= 32 && codePoint !== 127;
    })
        .join("");
}
function sanitizeFilenameComponent(value) {
    const normalizedValue = stripControlCharacters(value.normalize("NFKC"));
    return normalizedValue
        .replace(INVALID_FILENAME_CHARACTERS, " ")
        .replace(/\s+/g, " ")
        .trim()
        .replace(/[. ]+$/g, "");
}
function getAppNameFromMetadata(metadata) {
    if (!metadata || typeof metadata !== "object") {
        return null;
    }
    const appName = metadata.application?.name;
    if (typeof appName !== "string") {
        return null;
    }
    const sanitizedAppName = sanitizeFilenameComponent(appName);
    return sanitizedAppName.length > 0 ? sanitizedAppName : null;
}
export function getDownloadArchiveFilename(input) {
    const appNameFromMetadata = getAppNameFromMetadata(input.metadata);
    const rootDirName = sanitizeFilenameComponent(path.basename(input.rootDir));
    const appName = appNameFromMetadata || rootDirName || FALLBACK_APP_NAME;
    const timestamp = (input.timestamp ?? new Date())
        .toISOString()
        .replace(/[:.]/g, "-");
    return `${appName}-${timestamp}.zip`;
}
//# sourceMappingURL=download-filename.js.map