"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRemoteStream = createRemoteStream;
exports.isURL = isURL;
exports.guessAdapterFromFileName = guessAdapterFromFileName;
exports.supported = supported;
exports.generateMeta = generateMeta;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const fetchWithProxy_1 = __importDefault(require("../fetchWithProxy"));
async function createRemoteStream(urlIn) {
    const res = await (0, fetchWithProxy_1.default)(urlIn);
    if (!res.ok) {
        throw new Error(`Failed to fetch ${urlIn} status ${res.status} ${await res.text()}`);
    }
    return res;
}
function isURL(FileName) {
    let url;
    try {
        url = new URL(FileName);
    }
    catch (_) {
        return false;
    }
    return url.protocol === 'http:' || url.protocol === 'https:';
}
function makeLocation(location, protocol) {
    if (protocol === 'uri') {
        return {
            uri: location,
            locationType: 'UriLocation',
        };
    }
    if (protocol === 'localPath') {
        return {
            localPath: path_1.default.resolve(location),
            locationType: 'LocalPathLocation',
        };
    }
    throw new Error(`invalid protocol ${protocol}`);
}
function guessAdapterFromFileName(filePath) {
    const protocol = isURL(filePath) ? 'uri' : 'localPath';
    const name = path_1.default.basename(filePath);
    if (/\.vcf\.b?gz$/i.test(filePath)) {
        return {
            trackId: name,
            name: name,
            assemblyNames: [],
            adapter: {
                type: 'VcfTabixAdapter',
                vcfGzLocation: makeLocation(filePath, protocol),
            },
        };
    }
    else if (/\.gff3?\.b?gz$/i.test(filePath)) {
        return {
            trackId: name,
            name,
            assemblyNames: [],
            adapter: {
                type: 'Gff3TabixAdapter',
                gffGzLocation: makeLocation(filePath, protocol),
            },
        };
    }
    else if (/\.gtf?$/i.test(filePath)) {
        return {
            trackId: name,
            name,
            assemblyNames: [],
            adapter: {
                type: 'GtfAdapter',
                gtfLocation: { uri: filePath, locationType: 'UriLocation' },
            },
        };
    }
    else if (/\.vcf$/i.test(filePath)) {
        return {
            trackId: name,
            name,
            assemblyNames: [],
            adapter: {
                type: 'VcfAdapter',
                vcfLocation: makeLocation(filePath, protocol),
            },
        };
    }
    else if (/\.gff3?$/i.test(filePath)) {
        return {
            trackId: name,
            name,
            assemblyNames: [],
            adapter: {
                type: 'Gff3Adapter',
                gffLocation: makeLocation(filePath, protocol),
            },
        };
    }
    else {
        throw new Error(`Unsupported file type ${filePath}`);
    }
}
function supported(type = '') {
    return [
        'Gff3TabixAdapter',
        'VcfTabixAdapter',
        'Gff3Adapter',
        'VcfAdapter',
    ].includes(type);
}
async function generateMeta({ trackConfigs, attributes, outLocation, name, typesToExclude, assemblyNames, }) {
    const tracks = trackConfigs.map(({ adapter, textSearching, trackId }) => ({
        trackId,
        attributesIndexed: textSearching?.indexingAttributes || attributes,
        excludedTypes: textSearching?.indexingFeatureTypesToExclude || typesToExclude,
        adapterConf: adapter,
    }));
    fs_1.default.writeFileSync(path_1.default.join(outLocation, 'trix', `${name}_meta.json`), JSON.stringify({
        dateCreated: new Date().toISOString(),
        tracks,
        assemblyNames,
    }, null, 2));
}
