// When we import baseplate/html in the browser, we want to shim it to lit
// instead. This is because baseplate/html is a server-side only library and
// should not be used in the browser. This plugin will replace any imports of
// baseplate/html with lit, as well as any faceplate-ui server templates with
// their client-side counterparts.
const sourcesToMatch = [
    '@reddit/baseplate/html',
    '@reddit/baseplate/html.js',
    '@reddit/baseplate/html/html',
    '@reddit/baseplate/html/html.js',
    '@reddit/baseplate/html/index',
    '@reddit/baseplate/html/index.js',
];
export function replaceBaseplateHtmlWithLitPlugin() {
    const filesWithLitReplacements = new Set();
    const filesWithStrategyReplacements = new Set();
    return {
        name: 'replace-baseplate-html-with-lit',
        enforce: 'pre',
        /**
         * @param source {string}
         * @param importer {string}
         * @param options {unknown}
         * @return {Promise<undefined|*>}
         */
        async resolveId(source, importer, options) {
            if (importer?.endsWith('styleMap.js')) {
                console.log(importer, source);
            }
            if (sourcesToMatch.includes(source)) {
                console.log(`Resolving ${source} to Devvit's lit shim in ${importer}`);
                filesWithLitReplacements.add(importer);
                // @ts-expect-error - `this.resolve` exists in the context it runs in
                return await this.resolve('@devvit/ui-renderer/rollup/shims/lit', importer, options);
            }
            if (source.includes('@reddit/faceplate-ui/templates/server')) {
                const parts = source.split('@reddit/faceplate-ui/templates/server');
                const subpath = parts[1];
                const newSource = `@reddit/faceplate-ui/templates/client${subpath}`;
                console.log(`Resolving ${source} to '${newSource}' in ${importer}`);
                filesWithStrategyReplacements.add(importer);
                // @ts-expect-error - `this.resolve` exists in the context it runs in
                return await this.resolve(newSource, importer, options);
            }
            return undefined;
        },
        buildEnd() {
            if (filesWithLitReplacements.size > 0) {
                let litMsg = `The following files have had their references to baseplate/html shimmed to lit:\n`;
                filesWithLitReplacements.forEach((file) => (litMsg += `  - ${file}\n`));
                litMsg += `Baseplate's HTML rendering functions should not be used in the browser. Please consider updating these imports to point to lit directly when used in the browser. Please reach out to the Web Platform team if you need assistance.`;
                console.warn(litMsg);
            }
            if (filesWithStrategyReplacements.size > 0) {
                let strategyMsg = 'The following files are being used isomorphically but explicitly request server templates from faceplate-ui:\n';
                filesWithStrategyReplacements.forEach((file) => (strategyMsg += `  - ${file}\n`));
                strategyMsg += `Consult faceplate-ui's docs for how to properly use isomorphic templates: https://faceplate-ui.snooguts.net/?path=/docs/template-rendering--page`;
                console.warn(strategyMsg);
            }
        },
    };
}
//# sourceMappingURL=replaceBaseplateHtmlWithLit.js.map