"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTagInfo = void 0;
/* eslint-disable node/prefer-promises/fs */
const fs_1 = require("fs");
const path_1 = require("path");
const language_1 = require("./language");
const utils_1 = require("./utils");
const resolveSrc = (importerFile, srcPath) => path_1.resolve(path_1.dirname(importerFile), srcPath);
const getSrcContent = (file) => {
    return new Promise((resolve, reject) => {
        fs_1.readFile(file, (error, data) => {
            // istanbul ignore if
            if (error)
                reject(error);
            else
                resolve(data.toString());
        });
    });
};
async function doesFileExist(file) {
    return new Promise((resolve) => fs_1.access(file, 0, (err) => resolve(!err)));
}
const getTagInfo = async ({ attributes, filename, content, }) => {
    const dependencies = [];
    // catches empty content and self-closing tags
    const isEmptyContent = content == null || content.trim().length === 0;
    /** only include src file if content of tag is empty */
    if (attributes.src && isEmptyContent) {
        // istanbul ignore if
        if (typeof attributes.src !== 'string') {
            throw new Error('src attribute must be string');
        }
        let path = attributes.src;
        /** Only try to get local files (path starts with ./ or ../) */
        if (utils_1.isValidLocalPath(path)) {
            path = resolveSrc(filename, path);
            if (await doesFileExist(path)) {
                content = await getSrcContent(path);
                dependencies.push(path);
            }
            else {
                console.warn(`[svelte-preprocess] The file  "${path}" was not found.`);
            }
        }
    }
    const { lang, alias } = language_1.getLanguage(attributes);
    return {
        filename,
        attributes,
        content,
        lang,
        alias,
        dependencies,
    };
};
exports.getTagInfo = getTagInfo;
