import { ColumnBuilder } from "../../column-builder.js";
import { Column } from "../../column.js";
import { entityKind, is } from "../../entity.js";
import { ForeignKeyBuilder } from "../foreign-keys.js";
import { iife } from "../../tracing-utils.js";
import { makeCockroachArray, parseCockroachArray } from "../utils/array.js";
class CockroachColumnBuilder extends ColumnBuilder {
  foreignKeyConfigs = [];
  static [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 iife(
        (ref2, config2) => {
          const builder = new 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 [entityKind] = "CockroachColumnWithArrayBuilder";
  array(size) {
    return new CockroachArrayBuilder(
      this.config.name,
      this,
      size
    );
  }
}
class CockroachColumn extends Column {
  constructor(table, config) {
    super(table, config);
    this.table = table;
  }
  static [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 [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 [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 [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 [entityKind] = "CockroachArray";
  getSQLType() {
    return `${this.baseColumn.getSQLType()}[${typeof this.size === "number" ? this.size : ""}]`;
  }
  mapFromDriverValue(value) {
    if (typeof value === "string") {
      value = parseCockroachArray(value);
    }
    return value.map((v) => this.baseColumn.mapFromDriverValue(v));
  }
  mapToDriverValue(value, isNestedArray = false) {
    const a = value.map(
      (v) => v === null ? null : is(this.baseColumn, CockroachArray) ? this.baseColumn.mapToDriverValue(v, true) : this.baseColumn.mapToDriverValue(v)
    );
    if (isNestedArray) return a;
    return makeCockroachArray(a);
  }
}
export {
  CockroachArray,
  CockroachArrayBuilder,
  CockroachColumn,
  CockroachColumnBuilder,
  CockroachColumnWithArrayBuilder,
  ExtraConfigColumn,
  IndexedColumn
};
//# sourceMappingURL=common.js.map