import { r as registerInstance, e as writeTask, h, a as Host, g as getElement } from './index-55463a54.js';
import { c as createDefaultConfig, i as useLanguage, O as clone } from './ui-store-76bb0e8e.js';
import { S as SyncWithStore } from './index-dafb4b7f.js';

const rtkTranscriptsCss = ":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}:host{position:absolute;top:var(--rtk-space-4, 16px);right:var(--rtk-space-4, 16px);bottom:var(--rtk-space-4, 16px);left:var(--rtk-space-4, 16px);top:auto;display:flex;flex-direction:column;pointer-events:none;justify-content:center;width:100%;z-index:5}rtk-transcript{margin-top:var(--rtk-space-2, 8px)}";
const RtkTranscriptsStyle0 = rtkTranscriptsCss;

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const RtkTranscripts = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        /** Config object */
        this.config = createDefaultConfig();
        /** Language */
        this.t = useLanguage();
        this.transcripts = [];
        this.listenerAttached = false;
        this.onTranscript = (transcript) => {
            if (transcript.transcript) {
                this.add(transcript);
            }
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    addListener(meeting) {
        var _a;
        (_a = meeting === null || meeting === void 0 ? void 0 : meeting.ai) === null || _a === void 0 ? void 0 : _a.addListener('transcript', this.onTranscript);
        this.listenerAttached = true;
    }
    clearListeners(meeting) {
        var _a;
        this.onTranscript && ((_a = meeting === null || meeting === void 0 ? void 0 : meeting.ai) === null || _a === void 0 ? void 0 : _a.removeListener('transcript', this.onTranscript));
        this.listenerAttached = false;
        clearTimeout(this.disconnectTimeout);
        this.transcripts = [];
    }
    disconnectedCallback() {
        if (!this.meeting)
            return;
        this.clearListeners(this.meeting);
    }
    meetingChanged(meeting, oldMeeting) {
        clearTimeout(this.disconnectTimeout);
        if (oldMeeting)
            this.clearListeners(oldMeeting);
        if (!meeting)
            return;
        if (this.states.activeCaptions) {
            this.addListener(meeting);
        }
    }
    statesChanged(states) {
        if (!states)
            return;
        if (states.activeCaptions && !this.listenerAttached) {
            this.addListener(this.meeting);
        }
        if (!states.activeCaptions && this.listenerAttached) {
            this.clearListeners(this.meeting);
        }
    }
    transcriptionsReducer(acc, t) {
        if (!acc.length) {
            return [t];
        }
        let lastElement = acc[acc.length - 1];
        if (lastElement.peerId !== t.peerId) {
            return acc.concat(t);
        }
        if (lastElement.id === t.id) {
            lastElement.transcript = t.transcript;
            acc.pop();
            return acc.concat(clone(lastElement));
        }
        return acc.concat(t);
    }
    add(transcript) {
        // show transcripts only if tab is in focus and a maximum of 3 at a time
        // this.transcripts.splice(0, this.transcripts.length - 2);
        this.transcripts = this.transcriptionsReducer(this.transcripts, transcript);
    }
    remove(renderedId) {
        this.transcripts = this.transcripts.filter((transcript) => transcript.renderedId !== renderedId);
    }
    handleDismiss(e) {
        e.stopPropagation();
        const { id, renderedId } = e.detail;
        const el = this.host.shadowRoot.querySelector(`[data-id="${id}"]`);
        // exit animation
        el === null || el === void 0 ? void 0 : el.classList.add('exit');
        setTimeout(() => {
            writeTask(() => {
                this.remove(renderedId);
            });
        }, 400);
    }
    renderTranscripts() {
        const renderedTranscripts = [];
        this.transcripts.forEach((transcript) => {
            const t = {
                name: transcript.name,
                date: transcript.date,
                peerId: transcript.peerId,
                transcript: transcript.transcript,
                id: transcript.id,
                renderedId: transcript.id,
            };
            if (!renderedTranscripts.length) {
                transcript.renderedId = t.renderedId;
                renderedTranscripts.push(t);
                return;
            }
            const lastTranscript = renderedTranscripts[renderedTranscripts.length - 1];
            const maxTranscriptLength = 400;
            if (lastTranscript.transcript.length + t.transcript.length > maxTranscriptLength ||
                lastTranscript.peerId !== transcript.peerId) {
                transcript.renderedId = t.renderedId;
                renderedTranscripts.push(t);
                return;
            }
            lastTranscript.transcript += ' ' + transcript.transcript;
            transcript.renderedId = lastTranscript.renderedId;
        });
        renderedTranscripts.splice(0, renderedTranscripts.length - 2);
        return renderedTranscripts === null || renderedTranscripts === void 0 ? void 0 : renderedTranscripts.map((transcript) => (h("rtk-transcript", { key: transcript.id, "data-id": transcript.id, transcript: transcript, onRtkTranscriptDismiss: (e) => this.handleDismiss(e), t: this.t })));
    }
    render() {
        if (!this.states.activeCaptions)
            return;
        return h(Host, null, this.renderTranscripts());
    }
    get host() { return getElement(this); }
    static get watchers() { return {
        "meeting": ["meetingChanged"],
        "states": ["statesChanged"]
    }; }
};
__decorate([
    SyncWithStore()
], RtkTranscripts.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkTranscripts.prototype, "states", void 0);
__decorate([
    SyncWithStore()
], RtkTranscripts.prototype, "config", void 0);
__decorate([
    SyncWithStore()
], RtkTranscripts.prototype, "t", void 0);
RtkTranscripts.style = RtkTranscriptsStyle0;

export { RtkTranscripts as rtk_transcripts };
