"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertStreamPermissionsToChainPermission = exports.convertChainPermissionsToStreamPermissions = exports.streamPermissionToSolidityType = exports.toInternalPermissionAssignment = exports.isPublicPermissionAssignment = exports.toInternalPermissionQuery = exports.isPublicPermissionQuery = exports.PUBLIC_PERMISSION_USER_ID = exports.StreamPermission = void 0;
const utils_1 = require("@streamr/utils");
const ethers_1 = require("ethers");
var StreamPermission;
(function (StreamPermission) {
    StreamPermission["EDIT"] = "edit";
    StreamPermission["DELETE"] = "delete";
    StreamPermission["PUBLISH"] = "publish";
    StreamPermission["SUBSCRIBE"] = "subscribe";
    StreamPermission["GRANT"] = "grant";
})(StreamPermission || (exports.StreamPermission = StreamPermission = {}));
exports.PUBLIC_PERMISSION_USER_ID = '0x0000000000000000000000000000000000000000';
const isPublicPermissionQuery = (query) => {
    return query.public === true;
};
exports.isPublicPermissionQuery = isPublicPermissionQuery;
const toInternalPermissionQuery = async (query, streamIdBuilder) => {
    const typedBaseQuery = {
        ...query,
        streamId: await streamIdBuilder.toStreamID(query.streamId)
    };
    return ('userId' in typedBaseQuery)
        ? { ...typedBaseQuery, userId: (0, utils_1.toUserId)(typedBaseQuery.userId) }
        : typedBaseQuery;
};
exports.toInternalPermissionQuery = toInternalPermissionQuery;
const isPublicPermissionAssignment = (assignment) => {
    return assignment.public === true;
};
exports.isPublicPermissionAssignment = isPublicPermissionAssignment;
const toInternalPermissionAssignment = (assignment) => {
    return ('userId' in assignment)
        ? { ...assignment, userId: (0, utils_1.toUserId)(assignment.userId) }
        : assignment;
};
exports.toInternalPermissionAssignment = toInternalPermissionAssignment;
const streamPermissionToSolidityType = (permission) => {
    switch (permission) {
        case StreamPermission.EDIT:
            return 0n;
        case StreamPermission.DELETE:
            return 1n;
        case StreamPermission.PUBLISH:
            return 2n;
        case StreamPermission.SUBSCRIBE:
            return 3n;
        case StreamPermission.GRANT:
            return 4n;
        default:
            break;
    }
    return 0n;
};
exports.streamPermissionToSolidityType = streamPermissionToSolidityType;
const convertChainPermissionsToStreamPermissions = (chainPermissions) => {
    const now = Math.round(Date.now() / 1000);
    const permissions = [];
    if (chainPermissions.canEdit) {
        permissions.push(StreamPermission.EDIT);
    }
    if (chainPermissions.canDelete) {
        permissions.push(StreamPermission.DELETE);
    }
    if (chainPermissions.publishExpiration > now) {
        permissions.push(StreamPermission.PUBLISH);
    }
    if (chainPermissions.subscribeExpiration > now) {
        permissions.push(StreamPermission.SUBSCRIBE);
    }
    if (chainPermissions.canGrant) {
        permissions.push(StreamPermission.GRANT);
    }
    return permissions;
};
exports.convertChainPermissionsToStreamPermissions = convertChainPermissionsToStreamPermissions;
const convertStreamPermissionsToChainPermission = (permissions) => {
    return {
        canEdit: permissions.includes(StreamPermission.EDIT),
        canDelete: permissions.includes(StreamPermission.DELETE),
        publishExpiration: permissions.includes(StreamPermission.PUBLISH) ? ethers_1.MaxUint256 : 0n,
        subscribeExpiration: permissions.includes(StreamPermission.SUBSCRIBE) ? ethers_1.MaxUint256 : 0n,
        canGrant: permissions.includes(StreamPermission.GRANT)
    };
};
exports.convertStreamPermissionsToChainPermission = convertStreamPermissionsToChainPermission;
//# sourceMappingURL=permission.js.map