import { entityKind } from "../entity.js";
import { SelectionProxyHandler } from "../selection-proxy.js";
import { getTableColumns } from "../utils.js";
import { QueryBuilder } from "./query-builders/query-builder.js";
import { mssqlTable } from "./table.js";
import { MsSqlViewBase } from "./view-base.js";
import { MsSqlViewConfig } from "./view-common.js";
class ViewBuilderCore {
  constructor(name, schema) {
    this.name = name;
    this.schema = schema;
  }
  static [entityKind] = "MsSqlViewBuilder";
  config = {
    encryption: false,
    schemaBinding: false,
    viewMetadata: false
  };
  with(config) {
    this.config.encryption = config?.encryption;
    this.config.schemaBinding = config?.schemaBinding;
    this.config.viewMetadata = config?.viewMetadata;
    this.config.checkOption = config?.checkOption;
    return this;
  }
}
class ViewBuilder extends ViewBuilderCore {
  static [entityKind] = "MsSqlViewBuilder";
  as(qb) {
    if (typeof qb === "function") {
      qb = qb(new QueryBuilder());
    }
    const selectionProxy = new SelectionProxyHandler({
      alias: this.name,
      sqlBehavior: "error",
      sqlAliasedBehavior: "alias",
      replaceOriginalName: true
    });
    const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
    return new Proxy(
      new MsSqlView({
        mssqlConfig: this.config,
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: aliasedSelection,
          query: qb.getSQL().inlineParams()
        }
      }),
      selectionProxy
    );
  }
}
class ManualViewBuilder extends ViewBuilderCore {
  static [entityKind] = "MsSqlManualViewBuilder";
  columns;
  constructor(name, columns, schema) {
    super(name, schema);
    this.columns = getTableColumns(mssqlTable(name, columns));
  }
  existing() {
    return new Proxy(
      new MsSqlView({
        mssqlConfig: void 0,
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: this.columns,
          query: void 0
        }
      }),
      new SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
  as(query) {
    return new Proxy(
      new MsSqlView({
        mssqlConfig: this.config,
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: this.columns,
          query: query.inlineParams()
        }
      }),
      new SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
}
class MsSqlView extends MsSqlViewBase {
  static [entityKind] = "MsSqlView";
  [MsSqlViewConfig];
  constructor({ mssqlConfig, config }) {
    super(config);
    this[MsSqlViewConfig] = mssqlConfig;
  }
}
function mssqlViewWithSchema(name, selection, schema) {
  if (selection) {
    return new ManualViewBuilder(name, selection, schema);
  }
  return new ViewBuilder(name, schema);
}
function mssqlView(name, selection) {
  return mssqlViewWithSchema(name, selection, void 0);
}
export {
  ManualViewBuilder,
  MsSqlView,
  ViewBuilder,
  ViewBuilderCore,
  mssqlView,
  mssqlViewWithSchema
};
//# sourceMappingURL=view.js.map