using YMhut.Box.Core.Api;
using YMhut.Box.Core.Data;

namespace YMhut.Box.Core.Tools;

public interface IToolWorkerService
{
    Task<T> RunAsync<T>(Func<CancellationToken, Task<T>> work, CancellationToken cancellationToken = default);

    Task<ToolExecutionResult> ExecuteToolAsync(
        IToolModule module,
        string input,
        IApiManager? apiManager = null,
        IReferenceDataService? referenceDataService = null,
        CancellationToken cancellationToken = default,
        string language = "zh-CN");
}

public sealed class ToolWorkerService : IToolWorkerService
{
    public Task<T> RunAsync<T>(Func<CancellationToken, Task<T>> work, CancellationToken cancellationToken = default)
    {
        return Task.Run(() => work(cancellationToken), cancellationToken);
    }

    public Task<ToolExecutionResult> ExecuteToolAsync(
        IToolModule module,
        string input,
        IApiManager? apiManager = null,
        IReferenceDataService? referenceDataService = null,
        CancellationToken cancellationToken = default,
        string language = "zh-CN")
    {
        return RunAsync(
            token => ToolExecutor.ExecuteAsync(module, input, token, apiManager, referenceDataService, language),
            cancellationToken);
    }
}
