import { entityKind, is } from "../entity.js";
import { SelectionProxyHandler } from "../selection-proxy.js";
import { getTableColumns } from "../utils.js";
import { QueryBuilder } from "./query-builders/query-builder.js";
import { cockroachTable } from "./table.js";
import { CockroachViewBase } from "./view-base.js";
class DefaultViewBuilderCore {
  constructor(name, schema) {
    this.name = name;
    this.schema = schema;
  }
  static [entityKind] = "CockroachDefaultViewBuilderCore";
}
class ViewBuilder extends DefaultViewBuilderCore {
  static [entityKind] = "CockroachViewBuilder";
  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 CockroachView({
        config: {
          name: this.name,
          schema: this.schema,
          selectedFields: aliasedSelection,
          query: qb.getSQL().inlineParams()
        }
      }),
      selectionProxy
    );
  }
}
class ManualViewBuilder extends DefaultViewBuilderCore {
  static [entityKind] = "CockroachManualViewBuilder";
  columns;
  constructor(name, columns, schema) {
    super(name, schema);
    this.columns = getTableColumns(cockroachTable(name, columns));
  }
  existing() {
    return new Proxy(
      new CockroachView({
        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 CockroachView({
        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 MaterializedViewBuilderCore {
  constructor(name, schema) {
    this.name = name;
    this.schema = schema;
  }
  static [entityKind] = "CockroachMaterializedViewBuilderCore";
  config = {};
  withNoData() {
    this.config.withNoData = true;
    return this;
  }
}
class MaterializedViewBuilder extends MaterializedViewBuilderCore {
  static [entityKind] = "CockroachMaterializedViewBuilder";
  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 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 [entityKind] = "CockroachManualMaterializedViewBuilder";
  columns;
  constructor(name, columns, schema) {
    super(name, schema);
    this.columns = getTableColumns(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 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 SelectionProxyHandler({
        alias: this.name,
        sqlBehavior: "error",
        sqlAliasedBehavior: "alias",
        replaceOriginalName: true
      })
    );
  }
}
class CockroachView extends CockroachViewBase {
  static [entityKind] = "CockroachView";
  constructor({ config }) {
    super(config);
  }
}
const CockroachMaterializedViewConfig = Symbol.for("drizzle:CockroachMaterializedViewConfig");
class CockroachMaterializedView extends CockroachViewBase {
  static [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 is(obj, CockroachView);
}
function isCockroachMaterializedView(obj) {
  return is(obj, CockroachMaterializedView);
}
export {
  CockroachMaterializedView,
  CockroachMaterializedViewConfig,
  CockroachView,
  DefaultViewBuilderCore,
  ManualMaterializedViewBuilder,
  ManualViewBuilder,
  MaterializedViewBuilder,
  MaterializedViewBuilderCore,
  ViewBuilder,
  cockroachMaterializedView,
  cockroachMaterializedViewWithSchema,
  cockroachView,
  cockroachViewWithSchema,
  isCockroachMaterializedView,
  isCockroachView
};
//# sourceMappingURL=view.js.map