"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const debug_1 = __importDefault(require("debug"));
const debug = debug_1.default('localdevserver');
function isInNamespace(id, namespace) {
    return (id.startsWith(`${namespace}/`) || id.indexOf(`/${namespace}/`) !== -1);
}
function customComponentPlugin(namespace, fileSystemNamespace, srcDir) {
    const mappings = {};
    return {
        name: 'rollup-plugin-custom-components',
        resolveId(id) {
            debug(`[CustomComponent rollup plugin] resolveId called with ${id}`);
            if (isInNamespace(id, namespace)) {
                const componentName = path_1.default.basename(id);
                let jsFileName = null;
                const files = fast_glob_1.default
                    .sync(`**/${fileSystemNamespace}/${componentName}/${componentName}.*`, {
                    cwd: srcDir,
                    absolute: true,
                    ignore: ['**/__tests__/**']
                })
                    .forEach((value, _index, _array) => {
                    const fileName = path_1.default.normalize(value.toString());
                    let extension = path_1.default.extname(fileName);
                    if (extension === '.js') {
                        jsFileName = fileName;
                    }
                    mappings[`./${path_1.default.basename(fileName)}`] = fileName;
                });
                debug(`[CustomComponent rollup plugin] resolving as ${jsFileName}`);
                return jsFileName;
            }
            if (mappings[id]) {
                debug(`[CustomComponent rollup plugin] resolving as mapped ${mappings[id]}`);
                return mappings[id];
            }
            return null;
        }
    };
}
exports.customComponentPlugin = customComponentPlugin;
//# sourceMappingURL=rollup-plugin-custom-components.js.map