"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 view_exports = {};
__export(view_exports, {
  CockroachMaterializedView: () => CockroachMaterializedView,
  CockroachMaterializedViewConfig: () => CockroachMaterializedViewConfig,
  CockroachView: () => CockroachView,
  DefaultViewBuilderCore: () => DefaultViewBuilderCore,
  ManualMaterializedViewBuilder: () => ManualMaterializedViewBuilder,
  ManualViewBuilder: () => ManualViewBuilder,
  MaterializedViewBuilder: () => MaterializedViewBuilder,
  MaterializedViewBuilderCore: () => MaterializedViewBuilderCore,
  ViewBuilder: () => ViewBuilder,
  cockroachMaterializedView: () => cockroachMaterializedView,
  cockroachMaterializedViewWithSchema: () => cockroachMaterializedViewWithSchema,
  cockroachView: () => cockroachView,
  cockroachViewWithSchema: () => cockroachViewWithSchema,
  isCockroachMaterializedView: () => isCockroachMaterializedView,
  isCockroachView: () => isCockroachView
});
module.exports = __toCommonJS(view_exports);
var import_entity = require("../entity.cjs");
var import_selection_proxy = require("../selection-proxy.cjs");
var import_utils = require("../utils.cjs");
var import_query_builder = require("./query-builders/query-builder.cjs");
var import_table = require("./table.cjs");
var import_view_base = require("./view-base.cjs");
class DefaultViewBuilderCore {
  constructor(name, schema) {
    this.name = name;
    this.schema = schema;
  }
  static [import_entity.entityKind] = "CockroachDefaultViewBuilderCore";
}
class ViewBuilder extends DefaultViewBuilderCore {
  static [import_entity.entityKind] = "CockroachViewBuilder";
  as(qb) {
    if (typeof qb === "function") {
      qb = qb(new import_query_builder.QueryBuilder());
    }
    const selectionProxy = new import_selection_proxy.SelectionProxyHandler({
      alias: this.name,
      sqlBehavior: "error",
      sqlAliasedBehavior: "alias",
      replaceOriginalName: true
    });
    const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
    return new Proxy(
      new CockroachView({
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: aliasedSelection,
          query: qb.getSQL().inlineParams()
        }
      }),
      selectionProxy
    );
  }
}
class ManualViewBuilder extends DefaultViewBuilderCore {
  static [import_entity.entityKind] = "CockroachManualViewBuilder";
  columns;
  constructor(name, columns, schema) {
    super(name, schema);
    this.columns = (0, import_utils.getTableColumns)((0, import_table.cockroachTable)(name, columns));
  }
  existing() {
    return new Proxy(
      new CockroachView({
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: this.columns,
          query: void 0
        }
      }),
      new import_selection_proxy.SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
  as(query) {
    return new Proxy(
      new CockroachView({
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: this.columns,
          query: query.inlineParams()
        }
      }),
      new import_selection_proxy.SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
}
class MaterializedViewBuilderCore {
  constructor(name, schema) {
    this.name = name;
    this.schema = schema;
  }
  static [import_entity.entityKind] = "CockroachMaterializedViewBuilderCore";
  config = {};
  withNoData() {
    this.config.withNoData = true;
    return this;
  }
}
class MaterializedViewBuilder extends MaterializedViewBuilderCore {
  static [import_entity.entityKind] = "CockroachMaterializedViewBuilder";
  as(qb) {
    if (typeof qb === "function") {
      qb = qb(new import_query_builder.QueryBuilder());
    }
    const selectionProxy = new import_selection_proxy.SelectionProxyHandler({
      alias: this.name,
      sqlBehavior: "error",
      sqlAliasedBehavior: "alias",
      replaceOriginalName: true
    });
    const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
    return new Proxy(
      new CockroachMaterializedView({
        cockroachConfig: {
          withNoData: this.config.withNoData
        },
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: aliasedSelection,
          query: qb.getSQL().inlineParams()
        }
      }),
      selectionProxy
    );
  }
}
class ManualMaterializedViewBuilder extends MaterializedViewBuilderCore {
  static [import_entity.entityKind] = "CockroachManualMaterializedViewBuilder";
  columns;
  constructor(name, columns, schema) {
    super(name, schema);
    this.columns = (0, import_utils.getTableColumns)((0, import_table.cockroachTable)(name, columns));
  }
  existing() {
    return new Proxy(
      new CockroachMaterializedView({
        cockroachConfig: {
          withNoData: this.config.withNoData
        },
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: this.columns,
          query: void 0
        }
      }),
      new import_selection_proxy.SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
  as(query) {
    return new Proxy(
      new CockroachMaterializedView({
        cockroachConfig: {
          withNoData: this.config.withNoData
        },
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: this.columns,
          query: query.inlineParams()
        }
      }),
      new import_selection_proxy.SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
}
class CockroachView extends import_view_base.CockroachViewBase {
  static [import_entity.entityKind] = "CockroachView";
  constructor({ config }) {
    super(config);
  }
}
const CockroachMaterializedViewConfig = Symbol.for("drizzle:CockroachMaterializedViewConfig");
class CockroachMaterializedView extends import_view_base.CockroachViewBase {
  static [import_entity.entityKind] = "CockroachMaterializedView";
  [CockroachMaterializedViewConfig];
  constructor({ cockroachConfig, config }) {
    super(config);
    this[CockroachMaterializedViewConfig] = {
      withNoData: cockroachConfig?.withNoData
    };
  }
}
function cockroachViewWithSchema(name, selection, schema) {
  if (selection) {
    return new ManualViewBuilder(name, selection, schema);
  }
  return new ViewBuilder(name, schema);
}
function cockroachMaterializedViewWithSchema(name, selection, schema) {
  if (selection) {
    return new ManualMaterializedViewBuilder(name, selection, schema);
  }
  return new MaterializedViewBuilder(name, schema);
}
function cockroachView(name, columns) {
  return cockroachViewWithSchema(name, columns, void 0);
}
function cockroachMaterializedView(name, columns) {
  return cockroachMaterializedViewWithSchema(name, columns, void 0);
}
function isCockroachView(obj) {
  return (0, import_entity.is)(obj, CockroachView);
}
function isCockroachMaterializedView(obj) {
  return (0, import_entity.is)(obj, CockroachMaterializedView);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CockroachMaterializedView,
  CockroachMaterializedViewConfig,
  CockroachView,
  DefaultViewBuilderCore,
  ManualMaterializedViewBuilder,
  ManualViewBuilder,
  MaterializedViewBuilder,
  MaterializedViewBuilderCore,
  ViewBuilder,
  cockroachMaterializedView,
  cockroachMaterializedViewWithSchema,
  cockroachView,
  cockroachViewWithSchema,
  isCockroachMaterializedView,
  isCockroachView
});
//# sourceMappingURL=view.cjs.map