"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSettingsConfig = getSettingsConfig;
exports.isRemoteSettingsConfigured = isRemoteSettingsConfigured;
exports.create = create;
exports.get = get;
exports.getItem = getItem;
exports.setItem = setItem;
exports.deleteItem = deleteItem;
const ServerFactory_1 = require("../ServerFactory");
const requestsSetup_1 = require("./requestsSetup");
const yt = require('@ytsaurus/javascript-wrapper')();
function makeConfigError() {
    return new Error('Remote user settings are not configured. Please provide userSettingsConfig.');
}
function getSettingsConfig(cluster) {
    const { userSettingsConfig } = (0, ServerFactory_1.getApp)().config;
    return {
        cluster,
        ...userSettingsConfig,
    };
}
function getSettingsSetup(cluster) {
    return (0, requestsSetup_1.getRobotYTApiSetup)(getSettingsConfig(cluster).cluster).setup;
}
function isRemoteSettingsConfigured() {
    const { userSettingsConfig } = (0, ServerFactory_1.getApp)().config;
    return Boolean(userSettingsConfig);
}
function create({ ctx, username, cluster }) {
    ctx.log('settings.create', { username });
    if (!isRemoteSettingsConfigured()) {
        return Promise.reject(makeConfigError());
    }
    return yt.v3.create({
        setup: getSettingsSetup(cluster),
        parameters: {
            path: getSettingsConfig(cluster).mapNodePath + '/' + username,
            type: 'document',
            ignore_existing: true, // No error if exists
            attributes: {
                value: {},
            },
        },
    });
}
function get({ ctx, username, cluster }) {
    ctx.log('settings.get', { username, cluster });
    if (!isRemoteSettingsConfigured()) {
        return Promise.reject(makeConfigError());
    }
    return yt.v3.get({
        setup: getSettingsSetup(cluster),
        parameters: {
            path: getSettingsConfig(cluster).mapNodePath + '/' + username,
            output_format: {
                $value: 'json',
                $attributes: {
                    encode_utf8: 'false',
                },
            },
        },
    });
}
function getItem({ ctx, username, path, cluster }) {
    ctx.log('settings.getItem', { username, path });
    if (!isRemoteSettingsConfigured()) {
        return Promise.reject(makeConfigError());
    }
    return yt.v3.get({
        setup: getSettingsSetup(cluster),
        parameters: {
            path: getSettingsConfig(cluster).mapNodePath + '/' + username + '/' + path,
            output_format: {
                $value: 'json',
                $attributes: {
                    encode_utf8: 'false',
                },
            },
        },
    });
}
function setItem({ ctx, username, path, value, cluster }) {
    ctx.log('settings.setItem', { username, path, value, cluster });
    if (!isRemoteSettingsConfigured()) {
        return Promise.reject(makeConfigError());
    }
    return yt.v3.set({
        setup: getSettingsSetup(cluster),
        parameters: {
            path: getSettingsConfig(cluster).mapNodePath + '/' + username + '/' + path,
            input_format: {
                $value: 'json',
                $attributes: {
                    encode_utf8: 'false',
                },
            },
        },
        data: value,
    });
}
function deleteItem({ ctx, username, path, cluster }) {
    ctx.log('settings.deleteItem', { username, path, cluster });
    if (!isRemoteSettingsConfigured()) {
        return Promise.reject(makeConfigError());
    }
    return yt.v3.remove({
        setup: getSettingsSetup(cluster),
        parameters: {
            path: getSettingsConfig(cluster).mapNodePath + '/' + username + '/' + path,
        },
    });
}
