"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const lowcode_code_generator_1 = require("@alilc/lowcode-code-generator");
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const const_1 = require("./const");
const pluginFactory = (config) => {
    const cfg = Object.assign({ fileType: 'vue' }, config);
    const plugin = (pre) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        const next = Object.assign({}, pre);
        const ir = next.ir;
        if (!ir.state) {
            return next;
        }
        if (!ir.lifeCycles) {
            next.chunks.push({
                type: lowcode_code_generator_1.ChunkType.STRING,
                fileType: cfg.fileType,
                name: const_1.VUE_CHUNK_NAME.ScriptVueImp,
                content: `import { ref } from 'vue'`,
                linkAfter: [const_1.VUE_CHUNK_NAME.ScriptStart],
            });
        }
        const createStateContent = () => {
            let pieces = [];
            lodash_1.default.forIn(ir.state, (item, variable) => {
                if (item.type === 'JSExpression') {
                    const { value } = item;
                    const _ref = `const ${variable} = ref(${value});`;
                    pieces.push(_ref);
                }
            });
            return pieces.join('\n');
        };
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.ScriptInitState,
            content: createStateContent(),
            linkAfter: [const_1.VUE_CHUNK_NAME.ScriptVueImp],
        });
        return next;
    });
    return plugin;
};
exports.default = pluginFactory;
//# sourceMappingURL=initState.js.map