"use strict";
const fs = require("fs");
const utils_1 = require("./utils");
const guessEditor_1 = require("./guessEditor");
const openEditor_1 = require("./openEditor");
const error_1 = require("./error");
const enum_1 = require("./enum");
const launchEditor = async (file, options = {}) => {
    const { editor } = options;
    const { fileName, lineNumber, colNumber } = utils_1.parseFile(file);
    if (!fs.existsSync(fileName)) {
        return {
            success: false,
            message: `fileName: ${fileName} not existed`,
        };
    }
    const aliasEditor = enum_1.SUPPORTED_EDITTORS[editor];
    const guessedEditor = guessEditor_1.default(aliasEditor);
    if (!guessedEditor) {
        throw new error_1.default({
            editor: editor || 'UNKNOW',
            success: false,
            code: enum_1.ERROR_CODE.UNKNOWN,
        });
    }
    const { name, commands } = guessedEditor;
    utils_1.log('guessedEditor name', name);
    utils_1.log('guessedEditor commands', commands);
    const params = {
        fileName,
        lineNumber,
        colNumber,
    };
    const res = await openEditor_1.default({
        name,
        commands,
        ...params,
    });
    utils_1.log('launchEditor result', res);
    return res;
};
module.exports = launchEditor;
