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 { d as defineCustomElement$1 } from './p-3e0452c4.js';

const rtkCameraSelectorCss = ".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{margin-bottom:var(--rtk-space-2, 8px);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%}.group{margin-top:var(--rtk-space-0, 0px);margin-bottom:var(--rtk-space-0, 0px)}.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 RtkCameraSelectorStyle0 = rtkCameraSelectorCss;

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 RtkCameraSelector = /*@__PURE__*/ proxyCustomElement(class RtkCameraSelector extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** variant */
        this.variant = 'full';
        /** Language */
        this.t = useLanguage();
        this.videoDevices = [];
        this.canProduceVideo = true;
        this.stageStateListener = () => {
            this.canProduceVideo = this.meeting.self.permissions.canProduceVideo === 'ALLOWED';
        };
        this.deviceListUpdateListener = ({ devices }) => {
            this.videoDevices = devices.filter((device) => device.kind === 'videoinput');
        };
        this.deviceUpdateListener = ({ device }) => {
            if (device.kind !== 'videoinput')
                return;
            this.currentDevice = device;
        };
        this.mediaPermissionUpdateListener = async ({ kind, message }) => {
            if (!this.meeting)
                return;
            if (kind === 'video' && message === 'ACCEPTED') {
                this.videoDevices = await this.meeting.self.getVideoDevices();
            }
        };
    }
    meetingChanged(meeting) {
        var _a, _b, _c;
        if (!meeting)
            return;
        (_a = meeting.self) === null || _a === void 0 ? void 0 : _a.addListener('deviceListUpdate', this.deviceListUpdateListener);
        (_b = meeting.self) === null || _b === void 0 ? void 0 : _b.addListener('deviceUpdate', this.deviceUpdateListener);
        (_c = meeting.self) === null || _c === void 0 ? void 0 : _c.addListener('mediaPermissionUpdate', this.mediaPermissionUpdateListener);
        writeTask(async () => {
            var _a, _b;
            const videoDevices = await meeting.self.getVideoDevices();
            const currentVideoDevice = (_a = meeting.self.getCurrentDevices()) === null || _a === void 0 ? void 0 : _a.video;
            //  NOTE(callmetarush): Setting current video device to show on top of list
            if (currentVideoDevice != undefined) {
                this.videoDevices = [
                    (_b = videoDevices.find((device) => device.deviceId === currentVideoDevice.deviceId)) !== null && _b !== void 0 ? _b : currentVideoDevice,
                    ...videoDevices.filter((device) => device.deviceId !== currentVideoDevice.deviceId),
                ];
            }
            else {
                this.videoDevices = videoDevices;
            }
        });
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    disconnectedCallback() {
        var _a, _b, _c, _d, _e;
        (_b = (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.stage) === null || _b === void 0 ? void 0 : _b.removeListener('stageStatusUpdate', this.stageStateListener);
        (_c = this.meeting) === null || _c === void 0 ? void 0 : _c.self.removeListener('deviceListUpdate', this.deviceListUpdateListener);
        (_d = this.meeting) === null || _d === void 0 ? void 0 : _d.self.removeListener('deviceUpdate', this.deviceUpdateListener);
        (_e = this.meeting) === null || _e === void 0 ? void 0 : _e.self.removeListener('mediaPermissionUpdate', this.mediaPermissionUpdateListener);
    }
    async setDevice(deviceId) {
        var _a;
        const device = this.videoDevices.find((d) => d.deviceId === deviceId);
        this.currentDevice = device;
        if (device != null) {
            await ((_a = this.meeting) === null || _a === void 0 ? void 0 : _a.self.setDevice(device));
        }
    }
    render() {
        if (!this.meeting)
            return null;
        let unnamedVideoCount = 0;
        return (h(Host, null, this.canProduceVideo && (h("div", { class: 'group container ' + this.variant, part: "camera-selection" }, h("label", null, this.variant !== 'inline' && this.t('camera'), h("rtk-icon", { icon: this.iconPack.video_on, size: "sm" })), h("div", { class: "row" }, h("select", { class: "rtk-select", onChange: (e) => this.setDevice(e.target.value) }, this.videoDevices.map(({ deviceId, label }) => {
            var _a;
            return (h("option", { selected: ((_a = this.currentDevice) === null || _a === void 0 ? void 0 : _a.deviceId) === deviceId, value: deviceId }, label || `Camera ${++unnamedVideoCount}`));
        })))))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
    static get style() { return RtkCameraSelectorStyle0; }
}, [1, "rtk-camera-selector", {
        "meeting": [16],
        "size": [513],
        "iconPack": [16],
        "variant": [1],
        "t": [16],
        "videoDevices": [32],
        "currentDevice": [32],
        "canProduceVideo": [32]
    }, undefined, {
        "meeting": ["meetingChanged"]
    }]);
__decorate([
    SyncWithStore()
], RtkCameraSelector.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkCameraSelector.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkCameraSelector.prototype, "t", void 0);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-camera-selector", "rtk-icon"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-camera-selector":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkCameraSelector);
            }
            break;
        case "rtk-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { RtkCameraSelector as R, defineCustomElement as d };
