"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var common_exports = {};
__export(common_exports, {
  CockroachArray: () => CockroachArray,
  CockroachArrayBuilder: () => CockroachArrayBuilder,
  CockroachColumn: () => CockroachColumn,
  CockroachColumnBuilder: () => CockroachColumnBuilder,
  CockroachColumnWithArrayBuilder: () => CockroachColumnWithArrayBuilder,
  ExtraConfigColumn: () => ExtraConfigColumn,
  IndexedColumn: () => IndexedColumn
});
module.exports = __toCommonJS(common_exports);
var import_column_builder = require("../../column-builder.cjs");
var import_column = require("../../column.cjs");
var import_entity = require("../../entity.cjs");
var import_foreign_keys = require("../foreign-keys.cjs");
var import_tracing_utils = require("../../tracing-utils.cjs");
var import_array = require("../utils/array.cjs");
class CockroachColumnBuilder extends import_column_builder.ColumnBuilder {
  foreignKeyConfigs = [];
  static [import_entity.entityKind] = "CockroachColumnBuilder";
  references(ref, config = {}) {
    this.foreignKeyConfigs.push({ ref, config });
    return this;
  }
  unique(name) {
    this.config.isUnique = true;
    this.config.uniqueName = name;
    this.config.uniqueNameExplicit = name ? true : false;
    return this;
  }
  generatedAlwaysAs(as) {
    this.config.generated = {
      as,
      type: "always",
      mode: "stored"
    };
    return this;
  }
  /** @internal */
  buildForeignKeys(column, table) {
    return this.foreignKeyConfigs.map(({ ref, config }) => {
      return (0, import_tracing_utils.iife)(
        (ref2, config2) => {
          const builder = new import_foreign_keys.ForeignKeyBuilder(() => {
            const foreignColumn = ref2();
            return { name: config2.name, columns: [column], foreignColumns: [foreignColumn] };
          });
          if (config2.onUpdate) {
            builder.onUpdate(config2.onUpdate);
          }
          if (config2.onDelete) {
            builder.onDelete(config2.onDelete);
          }
          return builder.build(table);
        },
        ref,
        config
      );
    });
  }
  /** @internal */
  buildExtraConfigColumn(table) {
    return new ExtraConfigColumn(table, this.config);
  }
}
class CockroachColumnWithArrayBuilder extends CockroachColumnBuilder {
  static [import_entity.entityKind] = "CockroachColumnWithArrayBuilder";
  array(size) {
    return new CockroachArrayBuilder(
      this.config.name,
      this,
      size
    );
  }
}
class CockroachColumn extends import_column.Column {
  constructor(table, config) {
    super(table, config);
    this.table = table;
  }
  static [import_entity.entityKind] = "CockroachColumn";
  /** @internal */
  shouldDisableInsert() {
    return this.config.generatedIdentity !== void 0 && this.config.generatedIdentity.type === "always" || this.config.generated !== void 0 && this.config.generated.type !== "byDefault";
  }
}
class ExtraConfigColumn extends CockroachColumn {
  static [import_entity.entityKind] = "ExtraConfigColumn";
  getSQLType() {
    return this.getSQLType();
  }
  indexConfig = {
    order: this.config.order ?? "asc"
  };
  defaultConfig = {
    order: "asc"
  };
  asc() {
    this.indexConfig.order = "asc";
    return this;
  }
  desc() {
    this.indexConfig.order = "desc";
    return this;
  }
}
class IndexedColumn {
  static [import_entity.entityKind] = "IndexedColumn";
  constructor(name, keyAsName, type, indexConfig) {
    this.name = name;
    this.keyAsName = keyAsName;
    this.type = type;
    this.indexConfig = indexConfig;
  }
  name;
  keyAsName;
  type;
  indexConfig;
}
class CockroachArrayBuilder extends CockroachColumnWithArrayBuilder {
  static [import_entity.entityKind] = "CockroachArrayBuilder";
  constructor(name, baseBuilder, size) {
    super(name, "array", "CockroachArray");
    this.config.baseBuilder = baseBuilder;
    this.config.size = size;
  }
  /** @internal */
  build(table) {
    const baseColumn = this.config.baseBuilder.build(table);
    return new CockroachArray(
      table,
      this.config,
      baseColumn
    );
  }
}
class CockroachArray extends CockroachColumn {
  constructor(table, config, baseColumn, range) {
    super(table, config);
    this.baseColumn = baseColumn;
    this.range = range;
    this.size = config.size;
  }
  size;
  static [import_entity.entityKind] = "CockroachArray";
  getSQLType() {
    return `${this.baseColumn.getSQLType()}[${typeof this.size === "number" ? this.size : ""}]`;
  }
  mapFromDriverValue(value) {
    if (typeof value === "string") {
      value = (0, import_array.parseCockroachArray)(value);
    }
    return value.map((v) => this.baseColumn.mapFromDriverValue(v));
  }
  mapToDriverValue(value, isNestedArray = false) {
    const a = value.map(
      (v) => v === null ? null : (0, import_entity.is)(this.baseColumn, CockroachArray) ? this.baseColumn.mapToDriverValue(v, true) : this.baseColumn.mapToDriverValue(v)
    );
    if (isNestedArray) return a;
    return (0, import_array.makeCockroachArray)(a);
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CockroachArray,
  CockroachArrayBuilder,
  CockroachColumn,
  CockroachColumnBuilder,
  CockroachColumnWithArrayBuilder,
  ExtraConfigColumn,
  IndexedColumn
});
//# sourceMappingURL=common.cjs.map