"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 insert_exports = {};
__export(insert_exports, {
  MsSqlInsertBase: () => MsSqlInsertBase,
  MsSqlInsertBuilder: () => MsSqlInsertBuilder
});
module.exports = __toCommonJS(insert_exports);
var import_entity = require("../../entity.cjs");
var import_query_promise = require("../../query-promise.cjs");
var import_sql = require("../../sql/sql.cjs");
var import_table = require("../../table.cjs");
var import_utils = require("../../utils.cjs");
class MsSqlInsertBuilder {
  static [import_entity.entityKind] = "MsSqlInsertBuilder";
  config;
  table;
  session;
  dialect;
  constructor(table, session, dialect, output) {
    this.table = table;
    this.session = session;
    this.dialect = dialect;
    this.config = { table, output };
  }
  values(values) {
    values = Array.isArray(values) ? values : [values];
    if (values.length === 0) {
      throw new Error("values() must be called with at least one value");
    }
    const mappedValues = values.map((entry) => {
      const result = {};
      const cols = this.table[import_table.Table.Symbol.Columns];
      for (const colKey of Object.keys(entry)) {
        const colValue = entry[colKey];
        result[colKey] = (0, import_entity.is)(colValue, import_sql.SQL) ? colValue : new import_sql.Param(colValue, cols[colKey]);
      }
      return result;
    });
    return new MsSqlInsertBase(this.table, mappedValues, this.session, this.dialect, this.config.output);
  }
  output(fields = this.table[import_table.Table.Symbol.Columns]) {
    this.config.output = (0, import_utils.orderSelectedFields)(fields);
    return this;
  }
}
class MsSqlInsertBase extends import_query_promise.QueryPromise {
  constructor(table, values, session, dialect, output) {
    super();
    this.session = session;
    this.dialect = dialect;
    this.config = { table, values, output };
  }
  static [import_entity.entityKind] = "MsSqlInsert";
  config;
  /** @internal */
  getSQL() {
    return this.dialect.buildInsertQuery(this.config);
  }
  toSQL() {
    const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
    return rest;
  }
  prepare() {
    return this.session.prepareQuery(
      this.dialect.sqlToQuery(this.getSQL()),
      this.config.output
    );
  }
  execute(placeholderValues) {
    return this.prepare().execute(placeholderValues);
  }
  createIterator = () => {
    const self = this;
    return async function* (placeholderValues) {
      yield* self.prepare().iterator(placeholderValues);
    };
  };
  iterator = this.createIterator();
  // $dynamic(): MsSqlInsertDynamic<this> {
  // 	return this as any;
  // }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  MsSqlInsertBase,
  MsSqlInsertBuilder
});
//# sourceMappingURL=insert.cjs.map