export const Extensions = {
    JSON: 'json',
    PCD: 'pcd',
    PLY: 'ply',
    PB: 'pb',
    PB_GZ: 'pb.gz',
};
export const Prefixes = {
    Snapshot: 'visualization_snapshot',
};
class FileNameError extends Error {
    constructor(message, options) {
        super(message, options);
        this.name = 'FileNameError';
    }
}
const isExtension = (extension) => {
    return Object.values(Extensions).includes(extension);
};
const hasPrefix = (name) => {
    for (const prefix of Object.values(Prefixes)) {
        if (name.startsWith(prefix))
            return prefix;
    }
    return undefined;
};
const validatePrefix = (extension, prefix) => {
    switch (prefix) {
        case Prefixes.Snapshot: {
            if (extension !== Extensions.JSON &&
                extension !== Extensions.PB &&
                extension !== Extensions.PB_GZ) {
                return new FileNameError(`Only ${Extensions.JSON}, ${Extensions.PB} and ${Extensions.PB_GZ} snapshot files are supported.`);
            }
            break;
        }
    }
    return undefined;
};
export const parseFileName = (filename) => {
    const [name, ...extensions] = filename.split('.');
    const suffix = extensions.at(-1);
    if (!suffix) {
        return {
            success: false,
            error: new FileNameError('Could not determine file extension.'),
        };
    }
    const nested = extensions.at(-2);
    let extension = suffix;
    if (nested) {
        const nestedExtension = `${nested}.${suffix}`;
        if (isExtension(nestedExtension)) {
            extension = nestedExtension;
        }
    }
    if (!isExtension(extension)) {
        return {
            success: false,
            error: new FileNameError(`Only ${Object.values(Extensions).join(', ')} files are supported.`),
        };
    }
    const prefix = hasPrefix(name);
    if (prefix) {
        const error = validatePrefix(extension, prefix);
        if (error) {
            return {
                success: false,
                error,
            };
        }
        return { success: true, extension, prefix };
    }
    return { success: true, extension, prefix: undefined };
};
export const readFile = (file, reader, extension) => {
    if (!extension)
        return;
    switch (extension) {
        case Extensions.JSON: {
            return reader.readAsText(file);
        }
        case Extensions.PCD:
        case Extensions.PLY:
        case Extensions.PB:
        case Extensions.PB_GZ: {
            return reader.readAsArrayBuffer(file);
        }
    }
};
