/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { AbstractHelper } from "../AbstractHelper.js";
import { InterfaceListQuery } from "./InterfaceListQuery.js";
import { ObjectListQuery } from "./ObjectListQuery.js";
export class ListsHelper extends AbstractHelper {
  constructor(store, cacheKeys, whereCanonicalizer, orderByCanonicalizer, rdpCanonicalizer, intersectCanonicalizer, pivotCanonicalizer) {
    super(store, cacheKeys);
    this.whereCanonicalizer = whereCanonicalizer;
    this.orderByCanonicalizer = orderByCanonicalizer;
    this.rdpCanonicalizer = rdpCanonicalizer;
    this.intersectCanonicalizer = intersectCanonicalizer;
    this.pivotCanonicalizer = pivotCanonicalizer;
  }
  observe(options, subFn) {
    const ret = super.observe(options, subFn);
    if (options.streamUpdates) {
      ret.query.registerStreamUpdates(ret.subscription);
    }
    return ret;
  }
  getQuery(options) {
    const {
      type: typeDefinition,
      where,
      orderBy,
      withProperties,
      intersectWith,
      pivotTo
    } = options;
    const {
      apiName,
      type
    } = typeDefinition;
    const canonWhere = this.whereCanonicalizer.canonicalize(where ?? {});
    const canonOrderBy = this.orderByCanonicalizer.canonicalize(orderBy ?? {});
    const canonRdp = withProperties ? this.rdpCanonicalizer.canonicalize(withProperties) : undefined;
    const canonIntersect = intersectWith && intersectWith.length > 0 ? this.intersectCanonicalizer.canonicalize(intersectWith) : undefined;
    const canonPivot = pivotTo ? this.pivotCanonicalizer.canonicalize(apiName, pivotTo) : undefined;
    const listCacheKey = this.cacheKeys.get("list", type, apiName, canonWhere, canonOrderBy, canonRdp, canonIntersect, canonPivot);
    return this.store.queries.get(listCacheKey, () => {
      const QueryClass = type === "object" ? ObjectListQuery : InterfaceListQuery;
      return new QueryClass(this.store, this.store.subjects.get(listCacheKey), apiName, listCacheKey, options);
    });
  }
}
//# sourceMappingURL=ListsHelper.js.map