import type { ProviderConfig } from "@best-agent/protocol";
import {
  LocalApprovalStore,
  type ApprovalStore,
  type ApprovalStoreOptions,
} from "../core/approval-store.js";
import {
  LocalThreadStore,
  type ThreadStore,
  type ThreadStoreOptions,
} from "../core/thread-store.js";
import { createModelProvider, type ModelProvider } from "../provider/model-provider.js";

export function createDefaultThreadStore(storageRoot: string): ThreadStore {
  const storeOptions: ThreadStoreOptions = {
    storageRoot,
  };
  return new LocalThreadStore(storeOptions);
}

export function createDefaultApprovalStore(storageRoot: string): ApprovalStore {
  const storeOptions: ApprovalStoreOptions = {
    storageRoot,
  };
  return new LocalApprovalStore(storeOptions);
}

export function createRuntimeProvider(
  provider: ModelProvider | undefined,
  providerConfig?: ProviderConfig,
): ModelProvider | undefined {
  if (provider) {
    return provider;
  }

  if (!providerConfig) {
    return undefined;
  }

  return createModelProvider(providerConfig);
}

export async function closeResource(resource: {
  close?: () => void | Promise<void>;
}): Promise<void> {
  await resource.close?.();
}
