"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkIsDeveloper = checkIsDeveloper;
exports.makeCheckPermissionBatchSubRequest = makeCheckPermissionBatchSubRequest;
const constants_1 = require("../constants");
const yt = require('@ytsaurus/javascript-wrapper')();
function prepareCheckUserPermissionByAclRequests(groupName, user, permissionTypes) {
    return permissionTypes.map((permission) => {
        return {
            command: 'check_permission_by_acl',
            parameters: {
                acl: [
                    {
                        permissions: [permission],
                        subjects: [groupName],
                        action: 'allow',
                    },
                ],
                user,
                permission,
            },
        };
    });
}
function checkUserPermissionByAcl(groupName, user, permissionTypes, setup = undefined, ytApiId) {
    // $ yt --proxy cluster_proxy execute check_permission_by_acl \
    //   '{acl=[{permissions=[write]; subjects=[yt;admins]; action=allow}]; user=max42; permission=write}'
    const requests = prepareCheckUserPermissionByAclRequests(groupName, user, permissionTypes);
    return yt.v3.executeBatch({
        setup: { ...setup, requestHeaders: { [constants_1.YT_API_REQUEST_ID_HEADER]: ytApiId } },
        parameters: { requests },
    });
}
function checkIsDeveloper(login, setup, ytApiId) {
    return checkUserPermissionByAcl('admins', login, ['write'], setup, ytApiId)
        .then((d) => {
        const { output } = d[0];
        return output?.action === 'allow';
    })
        .catch(() => {
        return false;
    });
}
function makeCheckPermissionBatchSubRequest({ path, user, permission, transaction_id, vital, }) {
    const result = {
        command: 'check_permission',
        parameters: {
            path,
            user,
            permission,
            ...(transaction_id ? { transaction_id } : {}),
            ...(vital ? { vital } : {}),
        },
    };
    return result;
}
