"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stream = void 0;
const utils_1 = require("@streamr/utils");
const lodash_1 = require("lodash");
const range_1 = __importDefault(require("lodash/range"));
const StreamMetadata_1 = require("./StreamMetadata");
const permission_1 = require("./permission");
/**
 * A convenience API for managing and accessing an individual stream.
 *
 * @category Important
 */
class Stream {
    id;
    client;
    /** @internal */
    constructor(id, client) {
        this.id = id;
        this.client = client;
    }
    /**
     * See {@link StreamrClient.publish | StreamrClient.publish}.
     *
     * @category Important
     */
    publish(content, metadata) {
        return this.client.publish(this.id, content, metadata);
    }
    /**
     * See {@link StreamrClient.hasPermission | StreamrClient.hasPermission}.
     *
     * @category Important
     */
    async hasPermission(query) {
        return this.client.hasPermission({
            streamId: this.id,
            ...query
        });
    }
    /**
     * See {@link StreamrClient.getPermissions | StreamrClient.getPermissions}.
     *
     * @category Important
     */
    async getPermissions() {
        return this.client.getPermissions(this.id);
    }
    /**
     * See {@link StreamrClient.grantPermissions | StreamrClient.grantPermissions}.
     *
     * @category Important
     */
    async grantPermissions(...assignments) {
        return this.client.grantPermissions(this.id, ...assignments.map(permission_1.toInternalPermissionAssignment));
    }
    /**
     * See {@link StreamrClient.revokePermissions | StreamrClient.revokePermissions}.
     *
     * @category Important
     */
    async revokePermissions(...assignments) {
        return this.client.revokePermissions(this.id, ...assignments.map(permission_1.toInternalPermissionAssignment));
    }
    /**
     * See {@link StreamrClient.addStreamToStorageNode | StreamrClient.addStreamToStorageNode}.
     *
     * @category Important
     */
    async addToStorageNode(storageNodeAddress, opts = { wait: false }) {
        await this.client.addStreamToStorageNode(this.id, storageNodeAddress, opts);
    }
    /**
     * See {@link StreamrClient.removeStreamFromStorageNode | StreamrClient.removeStreamFromStorageNode}.
     */
    async removeFromStorageNode(nodeAddress) {
        return this.client.removeStreamFromStorageNode(this.id, (0, utils_1.toEthereumAddress)(nodeAddress));
    }
    /**
     * See {@link StreamrClient.getStorageNodes | StreamrClient.getStorageNodes}.
     */
    async getStorageNodes() {
        return this.client.getStorageNodes(this.id);
    }
    /**
     * Returns the partitions of the stream.
     */
    async getStreamParts() {
        return (0, range_1.default)(0, await this.getPartitionCount()).map((p) => (0, utils_1.toStreamPartID)(this.id, p));
    }
    async getPartitionCount() {
        return (0, StreamMetadata_1.getPartitionCount)(await this.getMetadata());
    }
    async getDescription() {
        const value = (await this.getMetadata()).description;
        if ((0, lodash_1.isString)(value)) {
            return value;
        }
        else {
            return undefined;
        }
    }
    async setDescription(description) {
        await this.setMetadata({
            ...await this.getMetadata(),
            description
        });
    }
    /**
     * Gets the value of `storageDays` field
     */
    async getStorageDayCount() {
        const value = (await this.getMetadata()).storageDays;
        if ((0, lodash_1.isNumber)(value)) {
            return value;
        }
        else {
            return undefined;
        }
    }
    /**
     * Sets the value of `storageDays` field
     */
    async setStorageDayCount(count) {
        await this.setMetadata({
            ...await this.getMetadata(),
            storageDays: count
        });
    }
    /**
     * Returns the metadata of the stream.
     */
    async getMetadata() {
        return this.client.getStreamMetadata(this.id);
    }
    /**
     * Updates the metadata of the stream.
     */
    async setMetadata(metadata) {
        await this.client.setStreamMetadata(this.id, metadata);
    }
}
exports.Stream = Stream;
//# sourceMappingURL=Stream.js.map