import {
  CUBE_API_SECRET,
  CUBE_API_URL
} from "./chunk-6I7ULIN6.js";
import {
  __decorateClass,
  __decorateParam
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/analytics/cube-backend.ts
import { Inject, Injectable, Logger } from "@nestjs/common";
var CubeAnalyticsBackend = class {
  constructor(apiUrl, apiSecret) {
    this.apiUrl = apiUrl;
    this.apiSecret = apiSecret;
  }
  apiUrl;
  apiSecret;
  logger = new Logger(CubeAnalyticsBackend.name);
  cubejsApi;
  async onModuleInit() {
    try {
      const { default: cubejs } = await import("@cubejs-client/core");
      this.cubejsApi = cubejs(this.apiSecret, { apiUrl: this.apiUrl });
    } catch {
      throw new Error(
        "CubeAnalyticsBackend requires @cubejs-client/core. Install it: bun add @cubejs-client/core"
      );
    }
  }
  async execute(cube, measures, dimensions, where, opts) {
    if (!this.cubejsApi) {
      this.logger.warn("Cube.js client not initialized \u2014 returning empty result");
      return [];
    }
    const query = {
      measures: measures.map((m) => cube + "." + m),
      dimensions: dimensions.map((d) => cube + "." + d)
    };
    if (where && Object.keys(where).length > 0) {
      query.filters = Object.entries(where).map(([member, value]) => ({
        member: cube + "." + member,
        operator: "equals",
        values: Array.isArray(value) ? value : [String(value)]
      }));
    }
    if (opts?.limit) {
      query.limit = opts.limit;
    }
    const resultSet = await this.cubejsApi.load(query);
    return resultSet.tablePivot();
  }
};
CubeAnalyticsBackend = __decorateClass([
  Injectable(),
  __decorateParam(0, Inject(CUBE_API_URL)),
  __decorateParam(1, Inject(CUBE_API_SECRET))
], CubeAnalyticsBackend);

export {
  CubeAnalyticsBackend
};
//# sourceMappingURL=chunk-7B3RYX45.js.map