"use strict";
exports.__esModule = true;
exports.dialect = exports.toProtoScalar = exports.toSchemaScalar = exports.joiBase = void 0;
var Joi = require("joi");
var cnst = require(".././const");
// defintions based on sqlite 3 datatypes
// https://www.sqlite.org/datatype3.html
exports.joiBase = function (type) {
    switch (type) {
        case "int":
        case "integer":
        case "tinyint":
        case "smallint":
        case "mediumint":
        case "bigint":
        case "unsigned big int":
        case "int2":
        case "int8":
            return Joi.number().integer();
        // character(n) -- ignore. use default
        // varchar(n) -- ignore. use default
        // varying character(n) -- ignore. use default
        // nchar(n) -- ignore. use default
        // native character(n) -- ignore. use default
        // nvarchar(n) -- ignore. use default
        case "text":
        case "clob":
            return Joi.string();
        case "blob":
        case "":
            return Joi.string();
        case "real":
        case "double":
        case "double precision":
        case "float":
            return Joi.number();
        case "numeric":
        // decimal(n1, n2) -- check for starts with
        case "boolean":
        case "date":
        case "datetime":
            return Joi.number();
        default:
            var match = type.match(cnst.REGEX_CHAR);
            if (match) {
                return Joi.string().max(Number(match.groups.len));
            }
            if (type.startsWith("decimal")) {
                return Joi.number();
            }
            return Joi.string();
    }
};
exports.toSchemaScalar = function (type) {
    switch (type) {
        case "int":
        case "integer":
        case "tinyint":
        case "smallint":
        case "mediumint":
        case "bigint":
        case "unsigned big int":
        case "int2":
        case "int8":
            return "Float";
        // character(n) -- ignore. use default
        // varchar(n) -- ignore. use default
        // varying character(n) -- ignore. use default
        // nchar(n) -- ignore. use default
        // native character(n) -- ignore. use default
        // nvarchar(n) -- ignore. use default
        case "text":
        case "clob":
            return "String";
        case "blob":
        case "":
            return "String";
        case "real":
        case "double":
        case "double precision":
        case "float":
            return "Float";
        case "numeric":
        // decimal(n1, n2) -- check for starts with
        case "boolean":
        case "date":
        case "datetime":
            return "Float";
        default:
            // this doesn't matter here as the schema cannot enforce
            // const match = type.match(cnst.REGEX_CHAR);
            // if (match) {
            //   return Joi.string().length(Number(match.groups.len));
            // }
            if (type.startsWith("decimal")) {
                return "Float";
            }
            return "String";
    }
};
exports.toProtoScalar = function (type) {
    switch (type) {
        case "int":
        case "integer":
        case "tinyint":
        case "smallint":
        case "mediumint":
        case "bigint":
        case "unsigned big int":
        case "int2":
        case "int8":
            return "uint32";
        // character(n) -- ignore. use default
        // varchar(n) -- ignore. use default
        // varying character(n) -- ignore. use default
        // nchar(n) -- ignore. use default
        // native character(n) -- ignore. use default
        // nvarchar(n) -- ignore. use default
        case "text":
        case "clob":
            return "string";
        case "blob":
        case "":
            return "string";
        case "real":
        case "double":
        case "double precision":
        case "float":
            return "uint32";
        case "numeric":
        // decimal(n1, n2) -- check for starts with
        case "boolean":
        case "date":
        case "datetime":
            return "uint32";
        default:
            // this doesn't matter here as the schema cannot enforce
            // const match = type.match(cnst.REGEX_CHAR);
            // if (match) {
            //   return Joi.string().length(Number(match.groups.len));
            // }
            if (type.startsWith("decimal")) {
                return "uint32";
            }
            return "string";
    }
};
exports.dialect = function (_a) {
    var migrationTable = _a.migrationTable;
    var dbSurveyQuery = "\n    SELECT\n        '' resource_schema,\n        m.type resource_type,\n        m.name resource_name,\n        p.cid resource_column_id,\n        p.name resource_column_name,\n        p.\"notnull\" \"notnull\",\n        lower(p.type) type,\n        p.pk primarykey,\n        CASE unique_idx.origin\n            WHEN 'u'\n                THEN 1\n            ELSE 0\n        END uniquekey\n      FROM\n        sqlite_master AS m\n      JOIN\n        pragma_table_info(m.name) AS p\n      left join (\n      SELECT\n        m.name resource_name,\n        ii.name resource_column_name,\n        il.\"unique\" uniquekey,\n        il.origin\n      FROM\n        sqlite_master AS m,\n        pragma_index_list(m.name) AS il,\n        pragma_index_info(il.name) AS ii\n      WHERE\n        m.type = 'table'\n        and il.origin = 'u'\n        and il.\"unique\" = 1\n    ) unique_idx\n      on\n          m.name = unique_idx.resource_name\n          and\n          p.name =unique_idx.resource_column_name\n      where\n        m.type in ('table', 'view')\n        and m.name not in ('" + migrationTable + "', '" + migrationTable + "_lock')\n      ORDER BY\n        m.name,\n        p.name;\n    ";
    var versionQuery = "select sqlite_version() as db_version;";
    return {
        dbSurveyQuery: dbSurveyQuery,
        versionQuery: versionQuery,
        joiBase: exports.joiBase,
        toSchemaScalar: exports.toSchemaScalar,
        toProtoScalar: exports.toProtoScalar
    };
};
//# sourceMappingURL=sqlite3.js.map