import _ from 'lodash';
import { createStreamableFile, getFallbackFilePath, getScopeDirectory, resolveRemoteFile, } from '../../../../code/tool/node/file.js';
import { parsePath, resolvePathRelativeToScope, } from '../../../../code/tool/shared/file.js';
import { cloneOptions } from '../../../../code/tool/shared/object.js';
// https://www.npmjs.com/package/ftp
/**
 * 1. Convert local files into streams.
 * 2. Leave remote file links.
 * 3. Leave content.
 */
export async function resolveInputForConvertRemoteNode(input) {
    const through = cloneOptions(input);
    if ('path' in through.input.file) {
        const inputPath = parsePath(through.input.file.path);
        if (inputPath.type === 'file-uri') {
            const file = await createStreamableFile(inputPath.href);
            _.unset(through.input.file, ['path']);
            _.set(through.input.file, ['content'], file);
        }
    }
    if (!through.output.file?.path) {
        const scope = getScopeDirectory(input.pathScope);
        const outputPath = resolvePathRelativeToScope(await getFallbackFilePath(input.output.file?.path, scope, through.output.format), scope);
        through.output.file = { path: outputPath };
    }
    return through;
}
/**
 * 1. Convert remote files into local files.
 * 2. Convert content to local files.
 * 3. Leave local file paths.
 */
export async function resolveInputForConvertLocalExternalNode(input) {
    const through = cloneOptions(input);
    if ('path' in through.input.file) {
        const inputPath = parsePath(through.input.file.path);
        switch (inputPath.type) {
            case 'https-uri':
            case 'http-uri': {
                const newInputPath = await resolveRemoteFile({
                    path: inputPath.href,
                    scope: getScopeDirectory(through.pathScope),
                    extension: through.output.format,
                });
                _.set(through.input.file, ['path'], newInputPath);
                break;
            }
        }
    }
    if ('content' in through.input.file) {
        // TODO: for the CLI.
    }
    if (!through.output.file?.path) {
        const scope = getScopeDirectory(input.pathScope);
        const outputPath = resolvePathRelativeToScope(await getFallbackFilePath(input.output.file?.path, scope, through.output.format), scope);
        through.output.file = { path: outputPath };
    }
    return through;
}
export async function resolveInputForConvertLocalInternalNode(input) {
    const through = cloneOptions(input);
    if ('path' in through.input.file) {
        const inputPath = parsePath(through.input.file.path);
        switch (inputPath.type) {
            case 'https-uri':
            case 'http-uri': {
                const newInputPath = await resolveRemoteFile({
                    path: inputPath.href,
                    scope: getScopeDirectory(through.pathScope),
                    extension: through.output.format,
                });
                _.set(through.input.file, ['path'], newInputPath);
                break;
            }
        }
    }
    if ('content' in through.input.file) {
        // TODO: for the CLI.
    }
    if (!through.output.file?.path) {
        const scope = getScopeDirectory(input.pathScope);
        const outputPath = resolvePathRelativeToScope(await getFallbackFilePath(input.output.file?.path, scope, through.output.format), scope);
        through.output.file = { path: outputPath };
    }
    return through;
}
//# sourceMappingURL=node.js.map