"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const utils_1 = require("@webruntime/server/dist/commonjs/utils/utils");
const Constants_1 = require("../Constants");
const RESOURCE_URL_PREFIX = '@salesforce/resourceUrl/';
const CONTENT_ASSET_URL_PREFIX = '@salesforce/contentAssetUrl/';
const debug = debug_1.default('localdevserver:resource');
function isResourceUrlScopedModule(id) {
    return id && id.startsWith(RESOURCE_URL_PREFIX);
}
function isContentAssetUrlScopedModule(id) {
    return id && id.startsWith(CONTENT_ASSET_URL_PREFIX);
}
function getExportUrl(id, buildDir, subDir) {
    const versionKey = utils_1.getLatestVersion(buildDir);
    const [resourceName] = id.split('/')[2].split('.');
    const replacement = `/assets/project/${versionKey}/${subDir}/${resourceName}`;
    debug(`resolving ${id} as ${replacement}`);
    return `export default '${replacement}';`;
}
function plugin(buildDir) {
    return {
        name: 'rollup-plugin-salesforce-resource-urls',
        load(id) {
            if (isResourceUrlScopedModule(id)) {
                return getExportUrl(id, buildDir, Constants_1.STATIC_RESOURCES);
            }
            if (isContentAssetUrlScopedModule(id)) {
                return getExportUrl(id, buildDir, Constants_1.CONTENT_ASSETS);
            }
            return null;
        },
        resolveId(id) {
            return isResourceUrlScopedModule(id) ||
                isContentAssetUrlScopedModule(id)
                ? id
                : null;
        }
    };
}
class ResourceUrlService {
    constructor({ buildDir }) {
        this.buildDir = buildDir;
    }
    getPlugin() {
        return plugin(this.buildDir);
    }
    async initialize() { }
    shutdown() { }
}
exports.ResourceUrlService = ResourceUrlService;
//# sourceMappingURL=ResourceUrlService.js.map