import { AxiosInstance } from "axios";
import { transformAndValidateSync } from "class-transformer-validator";
import { API_Config } from "./Types";

export class Config {
    private axios: AxiosInstance;
    private _config_promise: Promise<any>;

    constructor(axios: AxiosInstance) {
        this.axios = axios;
        // this.axios.defaults.timeout = 3000;
    }

    public async get(): Promise<API_Config> {
        // this will eventually call some API to pull the config, maybe?

        if (!this._config_promise) {
            const default_config: API_Config = {
                search: {
                    filters: [],
                    results_per_page: 10,
                    folders: [],
                },
                templates: [],
            };

            this._config_promise = new Promise(resolve => {
                setTimeout(() => {
                    resolve(default_config);
                }, 300);
            });
        }

        const response = await this._config_promise;
        transformAndValidateSync(API_Config, response);
        return response;
    }

    // TODO: figure out where config comes from
    // public async get(): Promise<API_Config> {
    //     if (!this._config_promise) {
    //         this._config_promise = this.axios.get("/content/v1/portal_config");
    //     }
    //     const response = await this._config_promise;
    //     transformAndValidateSync(API_Config, response.data);
    //     return response.data;
    // }
}
