"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveImportCode = void 0;
const utils_1 = require("@vuepress/utils");
const resolveImportCode = ({ importPath, lineStart, lineEnd }, { filePath }) => {
    let importFilePath = importPath;
    if (!utils_1.path.isAbsolute(importPath)) {
        // if the importPath is relative path, we need to resolve it
        // according to the markdown filePath
        if (!filePath) {
            return {
                importFilePath: null,
                importCode: 'Error when resolving path',
            };
        }
        importFilePath = utils_1.path.resolve(filePath, '..', importPath);
    }
    // check file existence
    if (!utils_1.fs.existsSync(importFilePath)) {
        return {
            importFilePath,
            importCode: 'File not found',
        };
    }
    // read file content
    const fileContent = utils_1.fs.readFileSync(importFilePath).toString();
    // resolve partial import
    return {
        importFilePath,
        importCode: fileContent
            .split('\n')
            .slice(lineStart ? lineStart - 1 : lineStart, lineEnd)
            .join('\n')
            .replace(/\n?$/, '\n'),
    };
};
exports.resolveImportCode = resolveImportCode;
