/*
 * 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 { createFetchHeaderMutator } from "@osdk/shared.net.fetch";
import { additionalContext } from "../Client.js";
import { createClientFromContext } from "../createClient.js";
import { OBSERVABLE_USER_AGENT } from "../util/UserAgent.js";
import { ObservableClientImpl } from "./internal/ObservableClientImpl.js";
import { Store } from "./internal/Store.js";
export let ObservableClient;
// TODO: Rename this from `ObserveObjectArgs` => `ObserveObjectCallbackArgs`. Not doing it now to reduce churn
// in repo.

// TODO: Rename this from `ObserveObjectsArgs` => `ObserveObjectsCallbackArgs`. Not doing it now to reduce churn

/**
 * User facing callback args for `observeLink`
 */

/**
 * Public interface for reactive data management with automatic updates.
 *
 * The ObservableClient provides a reactive data layer with:
 * - Real-time object and collection observation
 * - Automatic cache updates when data changes
 * - Optimistic updates for immediate UI feedback
 * - Pagination support for large collections
 * - Link traversal for relationship navigation
 */

export function createObservableClient(client) {
  // First we need a modified client that adds an extra header so we know its
  // an observable client
  const tweakedClient = createClientFromContext({
    ...client[additionalContext],
    fetch: createFetchHeaderMutator(client[additionalContext].fetch, headers => {
      headers.set("Fetch-User-Agent", [headers.get("Fetch-User-Agent"), OBSERVABLE_USER_AGENT].filter(x => x && x?.length > 0).join(" "));
      return headers;
    })
  });

  // Then we use that client instead. Because the `client` does not hold
  // any real state, this whole thing works.
  return new ObservableClientImpl(new Store(tweakedClient));
}
//# sourceMappingURL=ObservableClient.js.map