"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Webinar = exports.WebinarCSVStatus = exports.WebinarParticipant = exports.WebinarSession = exports.WebinarSessionParticipant = void 0;
const rxjs_1 = require("rxjs");
// declare let config: any;
class WebinarSessionParticipant {
    static create(id) { return new WebinarSessionParticipant(id); }
    constructor(id) {
        this.contact = null;
        this.activeSpeaker = false;
        this.mute = false;
        this.onStage = false;
        this.sharingOnStage = false;
        this.searchString = null;
        this.role = null;
        this.raiseHand = false;
        this.audioVideoSession = null;
        //public originalAudioVideoStream: MediaStream = null;
        this.originalAudioVideoStream = null;
        //public audioVideoStream: MediaStream = null;
        //public audioVideoStream: any = null;
        this.audioVideoSubStreamLevel = 1;
        //public audioVideoAudioElement: HTMLAudioElement = null;
        this.audioVideoAudioElement = null;
        this.sharingSession = null;
        this.sharingStream = null;
        //public sharingAudioElement: HTMLAudioElement = null;
        this.sharingAudioElement = null;
        this.medias = { audio: false, video: false, sharing: false };
        this.rxSubject = new rxjs_1.Subject();
        this.subStreamLevel = -1;
        this.id = id;
    }
}
exports.WebinarSessionParticipant = WebinarSessionParticipant;
class WebinarSession {
    constructor() {
        this.activeSpeakerId = null;
        this.localParticipant = null;
        this.speakerParticipants = [];
        this.attendeeParticipants = [];
        this.raiseHandAttendeeParticipants = [];
        this.masterMedia = null;
    }
    static create() { return new WebinarSession(); }
    getParticipantById(participantId) {
        if (this.localParticipant.id === participantId)
            return this.localParticipant;
        const speakerParticipant = this.speakerParticipants.find((part) => { return part.id === participantId; });
        if (speakerParticipant)
            return speakerParticipant;
        const attendeeParticipant = this.attendeeParticipants.find((part) => { return part.id === participantId; });
        if (attendeeParticipant)
            return attendeeParticipant;
        return null;
    }
    addOrUpdateParticipant(participant) {
        if (this.localParticipant.id === participant.id) {
            this.localParticipant.mute = participant.mute;
            this.localParticipant.rxSubject.next('updateInfo');
            return;
        }
        const participants = participant.role === 'attendee' ? this.attendeeParticipants : this.speakerParticipants;
        const participantIndex = participants.findIndex((part) => { return part.id === participant.id; });
        if (participantIndex !== -1) {
            const existingParticipant = participants[participantIndex];
            existingParticipant.mute = participant.mute;
            existingParticipant.rxSubject.next('updateInfo');
        }
        else {
            participants.push(participant);
        }
    }
    removeParticipant(participantId) {
        const participantIndex = this.speakerParticipants.findIndex((part) => { return part.id === participantId; });
        if (participantIndex !== -1) {
            this.speakerParticipants.splice(participantIndex, 1);
        }
        if (this.localParticipant && this.localParticipant.id === participantId) {
            this.localParticipant = null;
        }
    }
}
exports.WebinarSession = WebinarSession;
class WebinarParticipant {
    static create() { return new WebinarParticipant(); }
    getData() {
        return JSON.stringify({
            email: this.email,
            userId: this.userId,
            lastName: this.lastName,
            firstName: this.firstName,
            company: this.company,
            jobTitle: this.jobTitle
        });
    }
}
exports.WebinarParticipant = WebinarParticipant;
class WebinarCSVStatus {
    constructor() {
        this.okCount = 0;
        this.errorCount = 0;
        this.errorReport = [];
    }
    static create() { return new WebinarCSVStatus(); }
}
exports.WebinarCSVStatus = WebinarCSVStatus;
class Webinar {
    static create(_serverURL) {
        return new Webinar(_serverURL);
    }
    static createFromData(webinarData, _serverURL) {
        const webinar = new Webinar(_serverURL);
        Webinar.updateFromData(webinar, webinarData);
        return webinar;
    }
    static updateFromData(webinar, webinarData) {
        // Fill data
        if (webinarData.id) {
            webinar.id = webinarData.id;
        }
        if (webinarData.isActive) {
            webinar.status = 'inProgress';
        }
        if (webinarData.name) {
            webinar.name = webinarData.name;
        }
        if (webinarData.subject) {
            webinar.subject = webinarData.subject;
        }
        if (webinarData.webinarStartDate) {
            webinar.webinarStartDate = webinarData.webinarStartDate;
        }
        if (webinarData.webinarEndDate) {
            webinar.webinarEndDate = webinarData.webinarEndDate;
        }
        if (webinarData.waitingRoomStartDate) {
            webinar.waitingRoomStartDate = webinarData.waitingRoomStartDate;
        }
        if (webinarData.timeZone) {
            webinar.timeZone = webinarData.timeZone;
        }
        if (webinarData.roomId) {
            webinar.roomId = webinarData.roomId;
        }
        if (webinarData.roomModeratorsChatId) {
            webinar.roomModeratorsChatId = webinarData.roomModeratorsChatId;
        }
        if (webinarData.organizers) {
            webinar.organizers = webinarData.organizers;
        }
        if (webinarData.speakers) {
            webinar.speakers = webinarData.speakers;
        }
        if (webinarData.participants) {
            webinar.participants = webinarData.participants;
        }
        if (webinarData.creatorId) {
            webinar.creatorId = webinarData.creatorId;
        }
        if (webinarData.password) {
            webinar.password = webinarData.password;
        }
        if (webinarData.approvalRegistrationMethod) {
            webinar.approvalRegistrationMethod = webinarData.approvalRegistrationMethod;
        }
        if (webinarData.registrationUuid) {
            webinar.registrationUuid = webinarData.registrationUuid;
        }
        if (webinarData.emailNotification) {
            webinar.published = webinarData.emailNotification;
        }
        if (webinarData.moderatorsSelectedAnNotified) {
            webinar.moderatorsNotified = webinarData.moderatorsSelectedAnNotified;
        }
        if (webinarData.waitingRoomMultimediaURL) {
            webinar.waitingRoomMultimediaURLs = webinarData.waitingRoomMultimediaURL;
        }
        if (webinarData.stageBackground) {
            webinar.stageBackground = webinarData.stageBackground;
        }
        // Handle moderators
        webinar.organizers.forEach((organizer) => { organizer.role = 'organizer'; });
        webinar.speakers.forEach((speaker) => { speaker.role = 'speaker'; });
        if (webinarData.guestSpeakers) {
            webinarData.guestSpeakers.forEach((guestSpeaker) => {
                webinar.speakers.push({ status: 'invited', email: guestSpeaker.email, role: 'speaker' });
            });
        }
        webinar.pendingParticipantNumber = 0;
        webinar.acceptedParticipantNumber = 0;
        webinar.rejectedParticipantNumber = 0;
        webinar.participants.forEach((participant) => {
            switch (participant.state) {
                case 'pending':
                    webinar.pendingParticipantNumber++;
                    break;
                case 'approved':
                    webinar.acceptedParticipantNumber++;
                    break;
                case 'rejected':
                    webinar.rejectedParticipantNumber++;
                    break;
                default: break;
            }
        });
        // Compute default color
        if (webinar.name.length) {
            webinar.color = Webinar.computeWebinarColor(webinar.name);
        }
        // Compute webinarURL
        //webinar.webinarUrl = `${config.webservices.protocol}://webinar.${config.webservices.server.replace(".red", ".com")}/${webinar.registrationUuid}`;
        webinar.webinarUrl = webinar.serverURL; //`${config.webservices.protocol}://webinar.${config.webservices.server.replace(".red", ".com")}/${webinar.registrationUuid}`;
        /*
         if (window.location.origin.indexOf("localhost") !== -1) { webinar.webinarUrl = `localhost:9000/#/webinar/${webinar.registrationUuid}`; }
         if (window.location.origin.indexOf("demo") !== -1) { webinar.webinarUrl = `https://web-demo.openrainbow.org/#/webinar/${webinar.registrationUuid}`; }
         if (config.enableAioUrlMode) { webinar.webinarUrl = `${config.webservices.protocol}://${config.webservices.server.replace(".red", ".com")}/#/webinar/${webinar.registrationUuid}`; }
         // */
        //  Update tag status
        return webinar;
    }
    static computeWebinarColor(name) {
        const upperCaseWebinarName = name.toUpperCase();
        const colors = ["#d3a575", "#eb8d8d", "#d47ab0", "#b38deb", "#9aa3ed", "#76a6e5", "#92d0de", "#a7d6bc", "#99c86b", "#b4d43d", "#dcd33b", "#eec838"];
        let sum = 0;
        for (let i = 0; i < upperCaseWebinarName.length; i++) {
            sum += upperCaseWebinarName.charCodeAt(i);
        }
        return colors[sum % 12];
    }
    constructor(_serverURL) {
        this.id = null;
        this.name = '';
        this.subject = '';
        this.webinarStartDate = null;
        this.webinarEndDate = null;
        this.waitingRoomStartDate = null;
        this.timeZone = null;
        this.roomId = null;
        this.roomModeratorsChatId = null;
        //public room: Room = null;
        this.room = null;
        //public practiceRoom: Room = null;
        this.practiceRoom = null;
        this.organizers = [];
        this.speakers = [];
        this.participants = [];
        this.pendingParticipantNumber = 0;
        this.acceptedParticipantNumber = 0;
        this.rejectedParticipantNumber = 0;
        this.color = '';
        this.creatorId = null;
        this.isCreator = false;
        this.isOrganizer = false;
        this.isSpeaker = false;
        this.isAttendee = true;
        this.password = null;
        this.approvalRegistrationMethod = 'automatic';
        this.registrationUuid = null;
        this.published = false;
        this.moderatorsNotified = false;
        this.status = 'upcoming';
        this.webinarUrl = null;
        this.rxSubject = new rxjs_1.Subject();
        this.rxParticipantsSubject = new rxjs_1.Subject();
        this.isWebinarSync = true;
        this.session = null;
        this.waitingRoomMultimediaURLs = [];
        this.stageBackground = 'hop';
        this.blur = false;
        this.bgReplaceUrl = null;
        this.action = null;
        this.serverURL = _serverURL;
    }
    subscribe(handler) { return this.rxSubject.subscribe(handler); }
    getData() {
        const data = {
            name: this.name,
            subject: this.subject,
            webinarStartDate: this.webinarStartDate,
            webinarEndDate: this.webinarEndDate,
            waitingRoomStartDate: this.waitingRoomStartDate,
            timeZone: this.timeZone,
            isOrganizer: true,
            approvalRegistrationMethod: this.approvalRegistrationMethod,
            passwordNeeded: false,
            waitingRoomMultimediaURL: this.waitingRoomMultimediaURLs,
            stageBackground: this.stageBackground
        };
        return JSON.stringify(data);
    }
    updateRaiseHandParticipants() {
        if (this.session) {
            this.session.raiseHandAttendeeParticipants = this.session.attendeeParticipants.filter((participant) => { return participant.raiseHand; });
        }
    }
    getTagInfo() {
        switch (this.status) {
            case 'upcoming': return this.session ? { label: this.isAttendee ? 'webinarWaitingRoom' : 'webinarPractice', 'class': this.isAttendee ? 'waitingRoom' : 'practice' } : null;
            case 'inProgress': return { label: 'webinarLive', 'class': 'live' };
            case 'terminated': return { label: 'webinarTerminated', 'class': 'terminated' };
            default: return null;
        }
    }
}
exports.Webinar = Webinar;
module.exports.WebinarSessionParticipant = WebinarSessionParticipant;
module.exports.WebinarSession = WebinarSession;
module.exports.WebinarParticipant = WebinarParticipant;
module.exports.WebinarCSVStatus = WebinarCSVStatus;
module.exports.Webinar = Webinar;
//# sourceMappingURL=webinar.js.map