const { Service } = require('flitter-di')

class VueService extends Service {
    static get services() {
        return [...super.services, 'configs', 'utility']
    }

    data(merge = {}) {
        return {
            vue_state: {
                app_name: this.configs.get('app.name'),
                app_url: this.configs.get('app.url'),
                ...merge
            }
        }
    }

    session(req, merge = {}) {
        return {
            vue_session: this.utility.deep_merge({
                app: {
                    name: this.configs.get('app.name'),
                    url: this.configs.get('app.url'),
                },
                user: {
                    first_name: req.user.first_name,
                    last_name: req.user.last_name,
                    username: req.user.uid.toLowerCase(),
                    email: req.user.email,
                    tagline: req.user.tagline,
                    user_id: req.user.id,
                    has_mfa: req.user.mfa_enabled,
                },
            }, merge)
        }
    }

    invoke_action(res, action) {
        return res.page('public:action', {
            ...this.data({ action })
        })
    }

    auth_message(res, {message, next_destination, ...args}) {
        const text = args.button_text || 'Continue'
        return res.page('public:message', {
            ...this.data({
                message,
                actions: [
                    {
                        text,
                        action: 'redirect',
                        next: next_destination,
                    }
                ],
            })
        })
    }

    confirm(res, {message, yes, no, yes_text = 'Yes', no_text = 'No'}) {
        return res.page('public:message', {
            ...this.data({
                message,
                actions: [
                    {
                        text: no_text,
                        action: 'redirect',
                        next: no,
                    },
                    {
                        text: yes_text,
                        action: 'redirect',
                        next: yes,
                    }
                ],
            })
        })
    }
}

module.exports = exports = VueService
