import { Notes } from "../../../models/index.js";
import { IdentifiableError } from "../../../misc/identifiable-error.js";
/**
 * Stream channel
 */ export default class Channel {
    connection;
    id;
    static shouldShare;
    static requireCredential;
    get user() {
        return this.connection.user;
    }
    get userProfile() {
        return this.connection.userProfile;
    }
    get following() {
        return this.connection.following;
    }
    get muting() {
        return this.connection.muting;
    }
    get renoteMuting() {
        return this.connection.renoteMuting;
    }
    get blocking() {
        return this.connection.blocking;
    }
    get hidden() {
        return this.connection.hidden;
    }
    get followingChannels() {
        return this.connection.followingChannels;
    }
    get subscriber() {
        return this.connection.subscriber;
    }
    constructor(id, connection){
        this.id = id;
        this.connection = connection;
    }
    send(typeOrPayload, payload) {
        const type = payload === undefined ? typeOrPayload.type : typeOrPayload;
        const body = payload === undefined ? typeOrPayload.body : payload;
        this.connection.sendMessageToWs("channel", {
            id: this.id,
            type: type,
            body: body
        });
    }
    withPackedNote(callback) {
        return async (note)=>{
            try {
                // because `note` was previously JSON.stringify'ed, the fields that
                // were objects before are now strings and have to be restored or
                // removed from the object
                note.createdAt = new Date(note.createdAt);
                note.reply = undefined;
                note.renote = undefined;
                note.user = undefined;
                note.channel = undefined;
                const packed = await Notes.pack(note, this.user, {
                    detail: true
                });
                callback(packed);
            } catch (err) {
                if (err instanceof IdentifiableError && err.id === "9725d0ce-ba28-4dde-95a7-2cbb2c15de24") {
                    // skip: note not visible to user
                    return;
                } else {
                    throw err;
                }
            }
        };
    }
}
