const API = require("../../../lib/API")
const { BaseStdResponse } = require("../../../BaseStdResponse")
const { getEnabledUniLoginTypes, getUniLoginRuntimeConfig, VALID_SOCIAL_TYPES } = require('../../../lib/UniLoginClient')

class Options extends API {
    constructor() {
        super()

        this.setPath('/UniLogin/Options')
        this.setMethod('GET')
    }

    async onRequest(req, res) {
        try {
            const uniConfig = await getUniLoginRuntimeConfig()
            const enabledTypes = getEnabledUniLoginTypes(uniConfig)

            return res.json({
                ...BaseStdResponse.OK,
                data: {
                    configured: true,
                    enabledTypes,
                    methods: VALID_SOCIAL_TYPES.map(type => ({
                        type,
                        enabled: enabledTypes.includes(type)
                    }))
                }
            })
        } catch {
            return res.json({
                ...BaseStdResponse.OK,
                data: {
                    configured: false,
                    enabledTypes: [],
                    methods: VALID_SOCIAL_TYPES.map(type => ({
                        type,
                        enabled: false
                    }))
                }
            })
        }
    }
}

module.exports.Options = Options
