/*
 * 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 { asapScheduler, combineLatest, connectable, distinctUntilChanged, map, of, ReplaySubject, scheduled, switchMap } from "rxjs";
/**
 * Creates a connectable observable for a collection of objects
 *
 * @param subject The subject to connect to
 * @param store The store to use for resolving objects
 * @param createPayload A function that creates the payload from common parameters
 * @returns A connectable observable of the payload type
 */
export function createCollectionConnectable(subject, subjects, createPayload) {
  return connectable(subject.pipe(switchMap(listEntry => {
    const resolvedData = listEntry?.value?.data == null || listEntry.value.data.length === 0 ? of([]) : combineLatest(listEntry.value.data.map(cacheKey => subjects.get(cacheKey).pipe(map(objectEntry => objectEntry?.value), distinctUntilChanged())));
    return scheduled(combineLatest({
      resolvedData,
      isOptimistic: of(listEntry.isOptimistic),
      status: of(listEntry.status),
      lastUpdated: of(listEntry.lastUpdated)
    }).pipe(map(params => createPayload({
      resolvedData: Array.isArray(params.resolvedData) ? params.resolvedData : [],
      isOptimistic: params.isOptimistic,
      status: params.status,
      lastUpdated: params.lastUpdated
    }))), asapScheduler);
  })), {
    resetOnDisconnect: false,
    connector: () => new ReplaySubject(1)
  });
}
//# sourceMappingURL=createCollectionConnectable.js.map