"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../../../utils");
const SqlNode_1 = require("./SqlNode");
class ForeachSqlNode extends SqlNode_1.SqlNode {
    async getSql(paramMap, parameters = []) {
        this.parameters = parameters;
        // 提取数组
        const value = new Function(...Object.keys(paramMap), `return ${this.collection}`)(...Object.values(paramMap));
        // 如果集合数组，则过滤该节点
        if (value == null) {
            return '';
        }
        if (!(value instanceof Array)) {
            return '';
        }
        // 开始拼接SQL,
        const sqlBuilder = new utils_1.StringBuilder(this.open || '');
        const hasIndex = this.index != null && this.index.length > 0;
        // 获取数组长度
        const len = value.length;
        for (let i = 0; i < len; i++) {
            // 存入item对象
            paramMap[this.item] = value[i];
            // paramMap.put(this.item, value[i]);
            if (hasIndex) {
                // paramMap.put(this.index, i);
                paramMap[this.index] = i;
            }
            // 拼接子节点
            sqlBuilder.append(await this.executeChildren(paramMap, parameters));
            // 拼接分隔符
            if (i + 1 < len) {
                sqlBuilder.append(this.separator || '');
            }
        }
        // 拼接结束SQL
        sqlBuilder.append(this.close || '');
        return sqlBuilder.toString();
    }
}
exports.default = ForeachSqlNode;
//# sourceMappingURL=ForeachSqlNode.js.map