const { URL } = require('url');

module.exports = (req, res, { key, value, sk, retpath }) => {
    let url = new URL('/portal/set/any/', req.config.api.ya);

    url.searchParams.set('sk', sk);
    url.searchParams.set(key, value);

    if (retpath) {
        if (/^\/\//.test(retpath)) {
            retpath = `https:${retpath}`;
        }

        url.searchParams.set('retpath', retpath);
    } else {
        url.searchParams.set('empty', 1);
    }

    return res.redirect(url.href);
};
