import {BaseTokenManager} from "../base/baseTokenManager.js";
import {Requester} from "../utils/requester.js";

export class TikTokTokenManager extends BaseTokenManager {

    constructor({filepath, clientKey, clientSecret}) {
        super(filepath);
        this.clientKey = clientKey;
        this.clientSecret = clientSecret;
    }

    async renew() {
        let url = `https://www.tiktok.com/v2/auth/authorize?client_key=${this.clientKey}&response_type=code&scope=user.info.basic,video.list&redirect_uri=https://the-jailor.naaturel.be/oauth&state=SOME_RANDOM_STRING`
        let response = await Requester.doGetRequest(url);

        let userCode = response.code; //Might not be correct, please update

        url = "https://open.tiktokapis.com/v2/oauth/token/";
        let headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Cache-Control": "no-cache"
        }

        let params = {
            "client_key": this.clientKey,
            "client_secret": this.clientSecret,
            "code": userCode,
            "grant_type": "authorization_code",
            "redirect_uri": "https://the-jailor.naaturel.be/oauth"
        }

        let data = await Requester.doPostRequest(url, headers, params);
        let userId = data.open_id;
        let token = data.access_token;

        //Update token registry
    }
}