import { Inject, Singleton } from "typescript-ioc";
import CacheService, { CacheKey } from "./Cache";
import { Sync } from "./Decorators";
import { ExternalApiClient, GetConfigInfoResponse } from "./KTExternalApi";

/**
 * 应用服务
 */
@Singleton
export default class AppService {
    @Inject
    externalApiClient: ExternalApiClient;
    @Inject
    cacheService: CacheService;

    @Sync()
    async getConfigInfo(): Promise<GetConfigInfoResponse> {
        let configInfo = await this.cacheService.getConfigInfo();

        if (!configInfo) {
            configInfo = await this.externalApiClient.getConfigInfo();
            await this.cacheService.setConfigInfo(configInfo);
        }

        return configInfo;
    }
}

export { AppService }