import { randomBytes, createHash } from 'node:crypto';

export const state = () => randomBytes(8).toString('base64url');

export const pkce = () => {
    const codeVerifier = randomBytes(32).toString('base64url');
    const codeChallenge = createHash('sha256').update(codeVerifier).digest('base64url');
    return {
        codeVerifier,
        codeChallenge
    };
};

export const getRedirectUri = async ({fetch, isAppStudio = false}) => {
    if (!isAppStudio) {
        return 'http://localhost:8080/';
    }
    const res = await fetch('http://localhost:3001/AppStudio/api/getHostByPort?port=8080');
    const data = await res.json();
    return data.result;
};
