import { p as proxyCustomElement, H, w as writeTask, h, c as Host } from './p-ae373651.js';
import { e as defaultIconPack, i as useLanguage } from './p-5b3a7f00.js';
import { S as SyncWithStore } from './p-070aa695.js';
import { i as isFirefox } from './p-05186172.js';
import { d as defineCustomElement$2 } from './p-85587a5c.js';
import { d as defineCustomElement$1 } from './p-3e0452c4.js';

const rtkSpeakerSelectorCss = ".rtk-select{--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-700, 44 44 44) / var(--tw-bg-opacity));color:rgb(var(--rtk-colors-text-1000, 255 255 255))}.rtk-select:focus{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(0px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000)}.rtk-select{display:block;border-radius:var(--rtk-border-radius-sm, 4px);border-width:var(--rtk-border-width-none, 0);border-style:none;-webkit-appearance:none;-moz-appearance:none;appearance:none;padding:var(--rtk-space-3, 12px);font-size:16px;--icon-size:var(--rtk-select-chevron-size, var(--rtk-space-6, 24px));--icon-right-position:var(--rtk-select-chevron-right-position, var(--rtk-space-2, 8px));background-image:url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e\");background-position:right var(--icon-right-position) center;background-repeat:no-repeat;background-size:var(--icon-size) var(--icon-size);padding-right:calc(var(--icon-right-position) * 5);width:100%;max-width:100%;text-overflow:ellipsis}.inline .rtk-select{margin-top:var(--rtk-space-1, 4px);width:100%;padding-top:var(--rtk-space-1, 4px);padding-bottom:var(--rtk-space-1, 4px);padding-left:var(--rtk-space-1\\.5, 6px);padding-right:var(--rtk-space-1\\.5, 6px);padding-right:var(--rtk-space-8, 32px);font-size:14px}.row{display:flex;width:100%;align-items:center;justify-content:space-between;gap:var(--rtk-space-3, 12px)}.group{margin-top:var(--rtk-space-2, 8px);margin-bottom:var(--rtk-space-2, 8px)}.group>*{margin-bottom:var(--rtk-space-2, 8px)}.group>*:last-child{margin-bottom:var(--rtk-space-0, 0px)}.group select{flex:1 1 0%}.inline.group{margin-top:var(--rtk-space-0, 0px);margin-bottom:var(--rtk-space-0\\.5, 2px)}.inline.group>*{margin-bottom:var(--rtk-space-0, 0px)}label{display:flex;-webkit-user-select:none;-moz-user-select:none;user-select:none;align-items:center;gap:var(--rtk-space-1, 4px);font-size:14px}.inline .container{display:flex;align-items:center;justify-content:flex-start;gap:var(--rtk-space-2, 8px);padding-left:var(--rtk-space-2, 8px);padding-right:var(--rtk-space-2, 8px)}";
const RtkSpeakerSelectorStyle0 = rtkSpeakerSelectorCss;

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 RtkSpeakerSelector = /*@__PURE__*/ proxyCustomElement(class RtkSpeakerSelector extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        /** variant */
        this.variant = 'full';
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** Language */
        this.t = useLanguage();
        this.speakerDevices = [];
        this.currentDevices = { speaker: undefined };
        this.deviceListUpdateListener = async () => {
            const devices = await this.meeting.self.getSpeakerDevices();
            this.speakerDevices = devices;
        };
        this.deviceUpdateListener = ({ device }) => {
            if (device.kind === 'audiooutput') {
                this.currentDevices = {
                    speaker: device,
                };
            }
        };
        this.mediaPermissionUpdate = async ({ kind, message }) => {
            if (!this.meeting)
                return;
            if (kind === 'audio' && message === 'ACCEPTED') {
                this.speakerDevices = await this.meeting.self.getSpeakerDevices();
            }
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    disconnectedCallback() {
        var _a, _b, _c;
        (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.self.removeListener('deviceListUpdate', this.deviceListUpdateListener);
        (_b = this.meeting) === null || _b === void 0 ? void 0 : _b.self.removeListener('deviceUpdate', this.deviceUpdateListener);
        (_c = this.meeting) === null || _c === void 0 ? void 0 : _c.self.addListener('mediaPermissionUpdate', this.mediaPermissionUpdate);
    }
    meetingChanged(meeting) {
        if (!meeting)
            return;
        writeTask(async () => {
            var _a, _b;
            const { self } = meeting;
            const speakerDevices = await meeting.self.getSpeakerDevices();
            const currentSpeakerDevice = (_a = meeting.self.getCurrentDevices()) === null || _a === void 0 ? void 0 : _a.speaker;
            this.currentDevices = {
                speaker: currentSpeakerDevice,
            };
            self.addListener('deviceListUpdate', this.deviceListUpdateListener);
            self.addListener('deviceUpdate', this.deviceUpdateListener);
            self.addListener('mediaPermissionUpdate', this.mediaPermissionUpdate);
            if (currentSpeakerDevice != undefined) {
                this.speakerDevices = [
                    (_b = speakerDevices.find((device) => device.deviceId === currentSpeakerDevice.deviceId)) !== null && _b !== void 0 ? _b : currentSpeakerDevice,
                    ...speakerDevices.filter((device) => device.deviceId !== currentSpeakerDevice.deviceId),
                ];
            }
            else {
                this.speakerDevices = speakerDevices;
            }
        });
    }
    testAudio() {
        var _a;
        (_a = this.testAudioEl) === null || _a === void 0 ? void 0 : _a.play();
    }
    setDevice(deviceId) {
        var _a, _b;
        if (isFirefox(this.meeting))
            return;
        const device = this.speakerDevices.find((d) => d.deviceId === deviceId);
        this.currentDevices = {
            speaker: device,
        };
        if (device != null) {
            (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.self.setDevice(device);
            (_b = this.testAudioEl) === null || _b === void 0 ? void 0 : _b.setSinkId(device.deviceId);
        }
    }
    render() {
        if (!this.meeting)
            return null;
        let unnamedSpeakerCount = 0;
        return (h(Host, null, h("audio", { preload: "auto", src: "https://rtk-assets.realtime.cloudflare.com/ui-kit/speaker-test.mp3", ref: (el) => (this.testAudioEl = el) }), h("div", { class: 'group ' + this.variant, part: "speaker-selection" }, this.speakerDevices.length > 0 && !isFirefox(this.meeting) && (h("div", { class: "container" }, h("label", null, this.variant !== 'inline' && this.t('settings.speaker_output'), h("rtk-icon", { icon: this.iconPack.speaker, size: "sm" })), h("div", { class: "row" }, h("select", { class: "rtk-select", onChange: (e) => this.setDevice(e.target.value) }, this.speakerDevices.map(({ deviceId, label }) => {
            var _a;
            return (h("option", { value: deviceId, selected: ((_a = this.currentDevices.speaker) === null || _a === void 0 ? void 0 : _a.deviceId) === deviceId }, label || `Speaker ${++unnamedSpeakerCount}`));
        }))))), this.variant === 'full' && (h("rtk-button", { variant: "secondary", onClick: () => this.testAudio() }, h("rtk-icon", { icon: this.iconPack.speaker, slot: "start" }), this.t('test'))))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
    static get style() { return RtkSpeakerSelectorStyle0; }
}, [1, "rtk-speaker-selector", {
        "meeting": [16],
        "states": [16],
        "variant": [1],
        "size": [513],
        "iconPack": [16],
        "t": [16],
        "speakerDevices": [32],
        "currentDevices": [32]
    }, undefined, {
        "meeting": ["meetingChanged"]
    }]);
__decorate([
    SyncWithStore()
], RtkSpeakerSelector.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkSpeakerSelector.prototype, "states", void 0);
__decorate([
    SyncWithStore()
], RtkSpeakerSelector.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkSpeakerSelector.prototype, "t", void 0);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-speaker-selector", "rtk-button", "rtk-icon"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-speaker-selector":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkSpeakerSelector);
            }
            break;
        case "rtk-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "rtk-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { RtkSpeakerSelector as R, defineCustomElement as d };
