"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.trim_middle_comments = exports.merge_defaults = exports.default_command = exports.default_tags = exports.default_cost_pattern = exports.default_mock = exports.report = exports.get_all_ts_files = exports.find_all_nodes = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const typescript_1 = tslib_1.__importDefault(require("typescript"));
const shell_quote_1 = require("shell-quote");
const js_utils_1 = require("@xialvjun/js-utils");
exports.find_all_nodes = (sourceFile, cond) => {
    const result = [];
    function find(node) {
        if (cond(node)) {
            result.push(node);
            return;
        }
        else {
            typescript_1.default.forEachChild(node, find);
        }
    }
    find(sourceFile);
    return result;
};
exports.get_all_ts_files = (dirpath) => {
    let ts_files = [];
    const paths = fs_1.default.readdirSync(dirpath).map(it => path_1.default.join(dirpath, it));
    const path_stats = paths.map(it => [it, fs_1.default.statSync(it)]);
    const exts = [".ts", ".tsx"];
    const ts_folders = path_stats.filter(([p, s]) => (s.isDirectory() && path_1.default.basename(p) !== "node_modules") || (s.isFile() && exts.indexOf(path_1.default.extname(p)) > -1));
    ts_folders.forEach(([p, s]) => {
        if (s.isFile()) {
            ts_files.push(p);
        }
    });
    ts_folders.forEach(([p, s]) => {
        if (s.isDirectory()) {
            ts_files = ts_files.concat(exports.get_all_ts_files(p));
        }
    });
    return ts_files;
};
// ! level 不用 3, console 也不用 error, 不然做 输出管道 就很麻烦
function report(sourceFile, node, message, level = 1) {
    const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
    let str = `${sourceFile.fileName} (${line + 1},${character + 1}):\n${message}`
        .split("\n")
        .filter(v => v.trim())
        .map(it => "-- " + it)
        .join("\n");
    console[["", "info", "warn", "error"][level]](str);
}
exports.report = report;
exports.default_mock = "0";
exports.default_cost_pattern = /\(cost=\d+\.?\d*\.\.(\d+\.?\d*)/;
exports.default_tags = {
    sql: "sql",
    raw: "raw",
    cond: "cond",
    and: "and",
    or: "or",
    ins: "ins",
    upd: "upd",
    mock: "mock",
};
exports.default_command = `psql -c`;
exports.merge_defaults = (...configs) => {
    const config = js_utils_1.merge({
        mock: exports.default_mock,
        cost_pattern: exports.default_cost_pattern.source,
        command: exports.default_command,
        schema_command: "pg",
        tags: { ...exports.default_tags },
    }, ...configs);
    config.cost_pattern = config.cost_pattern || exports.default_cost_pattern.source;
    if (config.schema_command === "pg") {
        config.schema_command = `${config.command} ${shell_quote_1.quote([
            `copy (select table_schema, table_name, column_name from information_schema.columns WHERE table_schema=CURRENT_SCHEMA()) to stdout delimiter ','`,
        ])}`;
    }
    if (config.schema_command === "mysql") {
        config.schema_command = `${config.command} ${shell_quote_1.quote([
            `SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE()`,
        ])}`;
    }
    return config;
};
exports.trim_middle_comments = (q) => {
    let isHeadComment = true;
    return q
        .trim()
        .split("\n")
        .filter(l => l.trim().match(/^\-\-/) || l.trim().match(/^\/\*.*\*\/$/)
        ? isHeadComment
            ? true
            : false
        : ((isHeadComment = false), true))
        .join("\n");
};
//# sourceMappingURL=utils.js.map