/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tables_exports = {};
__export(tables_exports, {
  default: () => tables_default
});
module.exports = __toCommonJS(tables_exports);
var import_database = require("@nocobase/database");
var import_lodash = __toESM(require("lodash"));
async function getServerInstanceFromRequest(ctx) {
  const { resourceOf } = ctx.action;
  const serverInstance = await ctx.db.getRepository("databaseServers").findOne({
    filterByTk: resourceOf
  });
  if (!serverInstance) {
    throw new Error(`server ${resourceOf} not found`);
  }
  serverInstance.setApp(ctx.app);
  return serverInstance;
}
async function splitNameToNameWithSchema(tableNameString) {
  if (!tableNameString.includes(".")) {
    return {
      tableName: tableNameString
    };
  }
  const [schema, tableName] = tableNameString.split(".");
  return {
    schema,
    tableName
  };
}
function guessTableSequelizeAttribute(fields) {
  return {};
}
function columnAttribute(columnsInfo, columnName, indexes) {
  const columnInfo = columnsInfo[columnName];
  const attr = {
    type: columnInfo.type,
    allowNull: columnInfo.allowNull,
    primaryKey: columnInfo.primaryKey,
    unique: false
  };
  if (columnInfo.defaultValue && typeof columnInfo.defaultValue === "string") {
    const isSerial = columnInfo.defaultValue.match(/^nextval\(/);
    const isUUID = columnInfo.defaultValue.match(/^uuid_generate_v4\(/);
    if (!isSerial && !isUUID) {
      attr.defaultValue = columnInfo.defaultValue;
    }
  }
  for (const index of indexes) {
    if (index.fields.length == 1 && index.fields[0].attribute == columnName && index.unique) {
      attr.unique = true;
    }
  }
  if (attr.primaryKey && columnName == "id") {
    attr.autoIncrement = true;
  }
  return attr;
}
var tables_default = {
  name: "databaseServers.tables",
  actions: {
    // list tables on remote server
    async list(ctx, next) {
      const serverInstance = await getServerInstanceFromRequest(ctx);
      serverInstance.setApp(ctx.app);
      ctx.body = await serverInstance.listRemoteTables();
      await next();
    },
    // get remote table columns
    async get(ctx, next) {
      const serverInstance = await getServerInstanceFromRequest(ctx);
      serverInstance.setApp(ctx.app);
      const tableName = ctx.action.params.filterByTk;
      const tableInfo = await splitNameToNameWithSchema(tableName);
      const tableColumns = await serverInstance.describeTable(tableInfo);
      const indexes = await serverInstance.showIndexes(tableInfo);
      const unsupportedFields = [];
      const fields = [];
      import_lodash.default.forEach(tableColumns, (column, key) => {
        const columnInfo = {
          ...columnAttribute(tableColumns, key, indexes),
          ...import_database.ViewFieldInference.inferToFieldType({
            dialect: serverInstance.get("dialect"),
            type: column.type,
            name: key
          }),
          name: key,
          rawType: tableColumns[key].type
        };
        if (columnInfo.type) {
          fields.push(columnInfo);
        } else {
          unsupportedFields.push(columnInfo);
        }
      });
      const tableOptions = {
        fields,
        unsupportedFields
      };
      tableOptions.options = guessTableSequelizeAttribute(tableOptions.fields);
      ctx.body = tableOptions;
      await next();
    },
    async query(ctx, next) {
      const { filterByTk, fieldTypes, schema = "public", page = 1, pageSize = 10 } = ctx.action.params;
      const serverInstance = await getServerInstanceFromRequest(ctx);
      serverInstance.setApp(ctx.app);
      const tableInfo = await splitNameToNameWithSchema(filterByTk);
      const offset = (page - 1) * pageSize;
      const limit = 1 * pageSize;
      const sql = `SELECT *
                   FROM ${ctx.app.db.utils.quoteTable(
        ctx.app.db.utils.addSchema(tableInfo["tableName"], tableInfo["schema"])
      )} LIMIT ${limit}
                   OFFSET ${offset}`;
      const remoteDB = serverInstance.getRemoteDatabaseInstance();
      const rawValues = await remoteDB.sequelize.query(sql, { type: "SELECT" });
      await remoteDB.close();
      if (fieldTypes) {
        for (const raw of rawValues) {
          const fakeInstance = {
            dataValues: raw,
            getDataValue: (key) => raw[key]
          };
          for (const fieldName of Object.keys(fieldTypes)) {
            const fieldType = fieldTypes[fieldName];
            const FieldClass = ctx.app.db.fieldTypes.get(fieldType);
            const fieldOptions = new FieldClass(
              { name: fieldName },
              {
                db: ctx.app.db
              }
            ).options;
            if (fieldOptions.get) {
              const newValue = fieldOptions.get.apply(fakeInstance);
              raw[fieldName] = newValue;
            }
          }
        }
      }
      ctx.body = rawValues;
      await next();
    }
  }
};
