"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrimSqlNode = void 0;
const utils_1 = require("../../../utils");
const SqlNode_1 = require("./SqlNode");
class TrimSqlNode extends SqlNode_1.SqlNode {
    async getSql(paramMap, parameters = []) {
        this.parameters = parameters;
        const sqlBuffer = new utils_1.StringBuilder();
        let childrenSql = (await this.executeChildren(paramMap, parameters)).trim();
        if ((0, utils_1.isNotEmpty)(childrenSql)) {
            let upperSql = childrenSql.toUpperCase();
            // 开始拼接SQL,
            sqlBuffer.append(this.prefix || '').append(' ');
            //去掉prefixOverrides
            if ((0, utils_1.isNotEmpty)(this.prefixOverrides)) {
                const overrideArray = this.prefixOverrides.split('\\|');
                for (const override of overrideArray) {
                    if (upperSql.startsWith(override)) {
                        childrenSql = childrenSql
                            .substring(upperSql.indexOf(override) + override.length)
                            .trim();
                        upperSql = childrenSql.toUpperCase();
                        break;
                    }
                }
            }
            //去掉suffixOverrides
            if ((0, utils_1.isNotBlank)(this.suffixOverrides)) {
                const overrideArray = this.suffixOverrides.split('\\|');
                for (const override of overrideArray) {
                    if (upperSql.endsWith(override)) {
                        childrenSql = childrenSql.substring(0, upperSql.lastIndexOf(override));
                        break;
                    }
                }
            }
            sqlBuffer.append(childrenSql);
            // 拼接结束SQL
            sqlBuffer.append(' ').append(this.suffix || '');
        }
        return sqlBuffer.toString();
    }
}
exports.TrimSqlNode = TrimSqlNode;
//# sourceMappingURL=TrimSqlNode.js.map