/*
 * 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 { additionalContext } from "../../../Client.js";
import { API_NAME_IDX, INTERSECT_IDX, ListQuery, PIVOT_IDX, RDP_IDX, RIDS_IDX } from "./ListQuery.js";
export class ObjectListQuery extends ListQuery {
  createObjectSet(store) {
    const rdpConfig = this.cacheKey.otherKeys[RDP_IDX];
    const intersectWith = this.cacheKey.otherKeys[INTERSECT_IDX];
    const pivotInfo = this.cacheKey.otherKeys[PIVOT_IDX];
    const rids = this.cacheKey.otherKeys[RIDS_IDX];
    const clientCtx = store.client[additionalContext];
    const typeDefinition = {
      type: "object",
      apiName: this.apiName
    };
    if (pivotInfo != null) {
      let sourceSet = store.client({
        type: "object",
        apiName: pivotInfo.sourceType
      });

      // Filter source objects before pivoting to linked objects
      sourceSet = sourceSet.where(this.canonicalWhere);
      let objectSet = sourceSet.pivotTo(pivotInfo.linkName);
      if (rdpConfig != null) {
        objectSet = objectSet.withProperties(rdpConfig);
      }
      if (intersectWith != null && intersectWith.length > 0) {
        const intersectSets = intersectWith.map(whereClause => {
          let intersectSet = store.client({
            type: "object",
            apiName: pivotInfo.targetType
          });
          if (rdpConfig != null) {
            intersectSet = intersectSet.withProperties(rdpConfig);
          }
          return intersectSet.where(whereClause);
        });
        objectSet = objectSet.intersect(...intersectSets);
      }
      return objectSet;
    }

    // Start with either a static objectset (for RIDs) or a base objectset
    let objectSet;
    if (rids != null) {
      objectSet = clientCtx.objectSetFactory(typeDefinition, clientCtx, {
        type: "static",
        objects: [...rids]
      });
    } else {
      objectSet = store.client(typeDefinition);
    }
    if (rdpConfig != null) {
      objectSet = objectSet.withProperties(rdpConfig);
    }
    objectSet = objectSet.where(this.canonicalWhere);
    if (intersectWith != null && intersectWith.length > 0) {
      const intersectSets = intersectWith.map(whereClause => {
        let intersectSet = store.client({
          type: "object",
          apiName: this.apiName
        });
        if (rdpConfig != null) {
          intersectSet = intersectSet.withProperties(rdpConfig);
        }
        return intersectSet.where(whereClause);
      });
      objectSet = objectSet.intersect(...intersectSets);
    }
    return objectSet;
  }
  async revalidateObjectType(apiName) {
    if (this.apiName === apiName) {
      await this.revalidate(/* force */true);
    }
  }
  postProcessFetchedData(data) {
    return Promise.resolve(data);
  }
  extractRelevantObjects(changes) {
    return {
      added: {
        all: changes.addedObjects.get(this.cacheKey.otherKeys[API_NAME_IDX]) ?? [],
        strictMatches: new Set(),
        sortaMatches: new Set()
      },
      modified: {
        all: changes.modifiedObjects.get(this.cacheKey.otherKeys[API_NAME_IDX]) ?? [],
        strictMatches: new Set(),
        sortaMatches: new Set()
      }
    };
  }
}
//# sourceMappingURL=ObjectListQuery.js.map