import { readFileSync } from 'node:fs';
import { join } from 'node:path';
function isClassDeclaration(decl) {
    return decl.kind === 'class';
}
function isVersionStatic(member) {
    return !!member.static && member.name === 'version';
}
export function versionStaticFieldPlugin() {
    return {
        name: 'version-static-field-plugin',
        packageLinkPhase({ customElementsManifest }) {
            for (const mod of customElementsManifest.modules) {
                for (const decl of mod.declarations ?? []) {
                    if (isClassDeclaration(decl)) {
                        const versionField = decl.members?.find(isVersionStatic);
                        if (versionField) {
                            try {
                                const { version } = JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf-8'));
                                versionField.default = version;
                            }
                            catch (e) {
                                console.error(e);
                                delete versionField.default;
                            }
                        }
                    }
                }
            }
        },
    };
}
//# sourceMappingURL=version-static-field.js.map