"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddDeclarationMetaDataService = void 0;
const cyia_code_util_1 = require("cyia-code-util");
const static_injector_1 = require("static-injector");
const typescript_1 = __importDefault(require("typescript"));
const const_1 = require("./const");
const get_library_path_1 = require("./get-library-path");
const token_1 = require("./token");
const i0 = __importStar(require("static-injector"));
const i1 = __importStar(require("../mini-program-compiler"));
class AddDeclarationMetaDataService {
    constructor(entryPoint, miniProgramCompilerService) {
        this.entryPoint = entryPoint;
        this.directiveMap = miniProgramCompilerService.getDirectiveMap();
        this.componentMap = miniProgramCompilerService.getComponentMap();
    }
    run(dTsFileName, data) {
        const list = (0, cyia_code_util_1.createCssSelectorForTs)(data).queryAll(`ClassDeclaration`);
        return (data +
            this.addComponentMetaDataDeclaration(list) +
            this.addDirectiveMetaDataDeclaration(list));
    }
    addComponentMetaDataDeclaration(list) {
        const metaList = ['\n'];
        for (let i = 0; i < list.length; i++) {
            const classDeclaration = list[i];
            const isComponentClassDeclaration = classDeclaration.members.some((item) => {
                var _a;
                return typescript_1.default.isPropertyDeclaration(item) &&
                    ((_a = item.modifiers) === null || _a === void 0 ? void 0 : _a.some((modifier) => modifier.getText() === 'static')) &&
                    item.name.getText() === 'ɵcmp';
            });
            if (!isComponentClassDeclaration) {
                continue;
            }
            metaList.push(...this.getPropertyAndListener(classDeclaration, this.componentMap));
            const className = classDeclaration.name.getText();
            metaList.push(`declare const ${className}_${const_1.LIBRARY_COMPONENT_OUTPUT_PATH_SUFFIX}:"${(0, get_library_path_1.getComponentOutputPath)(this.entryPoint, className)}";`);
        }
        return metaList.join('\n');
    }
    addDirectiveMetaDataDeclaration(list) {
        const metaList = ['\n'];
        for (let i = 0; i < list.length; i++) {
            const classDeclaration = list[i];
            const isDirectiveClassDeclaration = classDeclaration.members.some((item) => {
                var _a;
                return typescript_1.default.isPropertyDeclaration(item) &&
                    ((_a = item.modifiers) === null || _a === void 0 ? void 0 : _a.some((modifier) => modifier.getText() === 'static')) &&
                    item.name.getText() === 'ɵdir';
            });
            if (!isDirectiveClassDeclaration) {
                continue;
            }
            metaList.push(...this.getPropertyAndListener(classDeclaration, this.directiveMap));
        }
        return metaList.join('\n');
    }
    getPropertyAndListener(classDeclaration, map) {
        const className = classDeclaration.name.getText();
        const list = [];
        for (const [key, meta] of map.entries()) {
            const directiveClassName = meta.name;
            if (directiveClassName === className) {
                const listeners = meta.host.listeners;
                list.push(`declare const ${className}_${const_1.LIBRARY_DIRECTIVE_LISTENERS_SUFFIX}:${JSON.stringify(Object.keys(listeners))};`);
                const properties = meta.host.properties;
                list.push(`declare const ${className}_${const_1.LIBRARY_DIRECTIVE_PROPERTIES_SUFFIX}:${JSON.stringify(Object.keys(properties))};`);
                break;
            }
        }
        return list;
    }
}
AddDeclarationMetaDataService.ɵfac = function AddDeclarationMetaDataService_Factory(t) { return new (t || AddDeclarationMetaDataService)(i0.ɵɵinject(token_1.ENTRY_POINT_TOKEN), i0.ɵɵinject(i1.MiniProgramCompilerService)); };
AddDeclarationMetaDataService.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: AddDeclarationMetaDataService, factory: AddDeclarationMetaDataService.ɵfac });
exports.AddDeclarationMetaDataService = AddDeclarationMetaDataService;
