/*
 * Copyright 2026 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.
 */

export function narrowObjectSet(objectSet, whereClause, linkedFilters) {
  let narrowed = Object.keys(whereClause).length === 0 ? objectSet : objectSet.where(whereClause);
  for (const filter of linkedFilters) {
    narrowed = narrowed.intersect(applyLinkedFilter(narrowed, filter));
  }
  return narrowed;
}
function applyLinkedFilter(base, filter) {
  // reverseLinkName's pointing back to Q is a semantic invariant we can't encode
  // in the type system, so the final result is cast to ObjectSet<Q>.
  return base.pivotTo(filter.linkName).where(filter.innerWhere).pivotTo(filter.reverseLinkName);
}
export function computeDualScopes(objectSet, whereClause, linkedFilters, showFilteredOutValues) {
  if (objectSet == null) {
    return {
      scoped: undefined,
      emptySource: undefined
    };
  }
  const scoped = narrowObjectSet(objectSet, whereClause, linkedFilters);
  if (!showFilteredOutValues || linkedFilters.length === 0) {
    return {
      scoped,
      emptySource: undefined
    };
  }
  return {
    scoped,
    emptySource: narrowObjectSet(objectSet, whereClause, [])
  };
}
//# sourceMappingURL=narrowObjectSet.js.map