"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertPresetToV2 = exports.convertThemeToV2 = exports.convertPresetToV1 = void 0;
const types_1 = require("../types");
exports.convertPresetToV1 = (exisitngPreset, preset) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    // Conversion Works only for V2 presets
    const convertedPreset = Object.assign({}, exisitngPreset);
    if (preset.permissions) {
        const modifiedPermissions = Object.assign({}, convertedPreset.role);
        if (preset.permissions.accept_waiting_requests !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { acceptWaitingRequests: preset.permissions.accept_waiting_requests });
        }
        if (preset.permissions.can_allow_participant_audio !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { disableParticipantAudio: preset.permissions.can_allow_participant_audio });
        }
        if (preset.permissions.can_allow_participant_screensharing !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { disableParticipantScreensharing: preset.permissions.can_allow_participant_screensharing });
        }
        if (preset.permissions.can_allow_participant_video !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { disableParticipantVideo: preset.permissions.can_allow_participant_video });
        }
        if (preset.permissions.kick_participant !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { kickParticipants: preset.permissions.kick_participant });
        }
        if (preset.permissions.pin_participant !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { pinParticipant: preset.permissions.pin_participant });
        }
        if (preset.permissions.can_record !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { allowRecording: preset.permissions.can_record });
        }
        if (preset.permissions.can_edit_display_name !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { editDisplayName: preset.permissions.can_edit_display_name });
        }
        if (preset.permissions.waiting_room_type) {
            modifiedPermissions.waitingRoom = types_1.WaitingRoomConversionToV1Type[preset.permissions.waiting_room_type];
        }
        if (preset.permissions.plugins) {
            modifiedPermissions.plugins = {
                close: preset.permissions.plugins.can_close,
                start: preset.permissions.plugins.can_start,
            };
        }
        if (preset.permissions.produce) {
            modifiedPermissions.produce = Object.assign(Object.assign({}, modifiedPermissions.produce), { video: preset.permissions.produce.video.allow, audio: preset.permissions.produce.audio, screenshare: preset.permissions.produce.screenshare.allow });
        }
        if (preset.permissions.polls) {
            modifiedPermissions.produce = Object.assign(Object.assign({}, modifiedPermissions.produce), { polls: preset.permissions.polls.can_create });
        }
        if (preset.permissions.chat) {
            modifiedPermissions.produce = Object.assign(Object.assign({}, modifiedPermissions.produce), { chat: {
                    text: preset.permissions.chat.public.text,
                    files: preset.permissions.chat.public.files,
                } });
        }
        if (preset.permissions.reactions !== undefined) {
            modifiedPermissions.produce = Object.assign(Object.assign({}, modifiedPermissions.produce), { reactions: preset.permissions.reactions });
        }
        if (preset.permissions.hidden_participant !== undefined) {
            modifiedPermissions.flags = Object.assign(Object.assign({}, modifiedPermissions.flags), { hiddenParticipant: preset.permissions.hidden_participant });
        }
        if (preset.permissions.is_recorder !== undefined) {
            modifiedPermissions.flags = Object.assign(Object.assign({}, modifiedPermissions.flags), { recorder: preset.permissions.is_recorder });
        }
        if (preset.permissions.view_type === types_1.DyteViewTypes.webinar) {
            modifiedPermissions.webinar = Object.assign(Object.assign({}, modifiedPermissions.webinar), { viewOnly: preset.permissions.request_produce });
            convertedPreset.roomViewType = 'webinar';
        }
        if (preset.permissions.accept_present_requests !== undefined) {
            modifiedPermissions.permissions = Object.assign(Object.assign({}, modifiedPermissions.permissions), { acceptWaitingRequests: preset.permissions.accept_present_requests });
            modifiedPermissions.webinar = Object.assign(Object.assign({}, modifiedPermissions.webinar), { viewOnly: !preset.permissions.accept_present_requests });
            convertedPreset.roomViewType = 'webinar';
        }
        convertedPreset["role"] = modifiedPermissions;
    }
    if (preset.theme) {
        const modifiedUiConfig = Object.assign({}, convertedPreset.uiConfig);
        if (preset.theme.alone_here) {
            modifiedUiConfig["aloneHereElement"] = !!(preset.theme.alone_here);
        }
        if (preset.theme.waiting_room) {
            modifiedUiConfig["waitingRoom"] = {
                showSelf: preset.theme.waiting_room.enable_preview,
                roomElement: preset.theme.waiting_room.element
            };
        }
        if (preset.theme.control_bar) {
            modifiedUiConfig["controlBar"] = preset.theme.control_bar.is_enabled;
            modifiedUiConfig["controlBarElements"] = {
                share: (_a = preset.theme.control_bar.elements) === null || _a === void 0 ? void 0 : _a.invite,
                screenShare: (_b = preset.theme.control_bar.elements) === null || _b === void 0 ? void 0 : _b.screenshare,
                plugins: (_c = preset.theme.control_bar.elements) === null || _c === void 0 ? void 0 : _c.plugins,
                participants: (_d = preset.theme.control_bar.elements) === null || _d === void 0 ? void 0 : _d.participants,
                chat: (_e = preset.theme.control_bar.elements) === null || _e === void 0 ? void 0 : _e.chat,
                polls: (_f = preset.theme.control_bar.elements) === null || _f === void 0 ? void 0 : _f.polls,
                fullscreen: (_g = preset.theme.control_bar.elements) === null || _g === void 0 ? void 0 : _g.fullscreen,
            };
        }
        if (preset.theme.header) {
            modifiedUiConfig["header"] = preset.theme.header.is_enabled;
            modifiedUiConfig["headerElements"] = {
                clock: (_h = preset.theme.header.elements) === null || _h === void 0 ? void 0 : _h.timer,
                title: (_j = preset.theme.header.elements) === null || _j === void 0 ? void 0 : _j.title,
                logo: !!((_k = preset.theme.header.elements) === null || _k === void 0 ? void 0 : _k.logo),
                participantCount: (_l = preset.theme.header.elements) === null || _l === void 0 ? void 0 : _l.participant_count,
            };
            modifiedUiConfig["logo"] = (_m = preset.theme.header.elements) === null || _m === void 0 ? void 0 : _m.logo;
        }
        if (preset.theme.auto_tune) {
            modifiedUiConfig.grid = Object.assign({}, preset.theme.grid);
        }
        if (preset.theme.grid) {
            modifiedUiConfig.grid = Object.assign({}, preset.theme.grid);
        }
        if (preset.theme.colors) {
            modifiedUiConfig['colors'] = {
                primary: preset.theme.colors.primary,
                secondary: preset.theme.colors.secondary,
                textPrimary: preset.theme.colors.text,
                videoBackground: preset.theme.colors.background,
            };
        }
        if (preset.theme.dimensions) {
            modifiedUiConfig['dimensions'] = {
                width: preset.theme.dimensions.width,
                height: preset.theme.dimensions.height,
            };
        }
        convertedPreset["uiConfig"] = modifiedUiConfig;
    }
    return convertedPreset;
};
exports.convertThemeToV2 = (parentTheme, oldTheme) => {
    var _a;
    const newTheme = Object.assign({}, parentTheme);
    const controlBarBody = Object.assign(Object.assign({}, newTheme.control_bar), { is_enabled: oldTheme.controlBar !== undefined ? oldTheme.controlBar
            : newTheme.control_bar.is_enabled });
    const headerBody = Object.assign(Object.assign({}, newTheme.header), { is_enabled: oldTheme.header !== undefined ? oldTheme.header
            : newTheme.header.is_enabled });
    if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements) {
        const elements = Object.assign({}, controlBarBody.elements);
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.chat) !== undefined) {
            elements.chat = oldTheme.controlBarElements.chat;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.fullscreen) !== undefined) {
            elements.fullscreen = oldTheme.controlBarElements.fullscreen;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.participants) !== undefined) {
            elements.participants = oldTheme.controlBarElements.participants;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.plugins) !== undefined) {
            elements.plugins = oldTheme.controlBarElements.plugins;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.polls) !== undefined) {
            elements.polls = oldTheme.controlBarElements.polls;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.screenShare) !== undefined) {
            elements.screenshare = oldTheme.controlBarElements.screenShare;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.controlBarElements.share) !== undefined) {
            elements.invite = oldTheme.controlBarElements.share;
        }
        elements.reactions = false;
        elements.layout = false;
        controlBarBody.elements = elements;
    }
    if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.headerElements) {
        const elements = Object.assign({}, headerBody.elements);
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.headerElements.clock) !== undefined) {
            elements.timer = oldTheme.headerElements.clock;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.headerElements.participantCount) !== undefined) {
            elements.participant_count = oldTheme.headerElements.participantCount;
        }
        if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.headerElements.title) !== undefined) {
            elements.title = oldTheme.headerElements.title;
        }
        elements.change_layout = false;
        headerBody.elements = elements;
    }
    if ((oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.logo) !== undefined && oldTheme.logo !== '') {
        headerBody.elements.logo = oldTheme.logo;
    }
    newTheme.header = headerBody;
    newTheme.control_bar = controlBarBody;
    if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.colors) {
        const colorBody = Object.assign({}, newTheme.colors);
        if (oldTheme.colors.primary) {
            colorBody.primary = oldTheme.colors.primary;
        }
        if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.colors.secondary) {
            colorBody.secondary = oldTheme.colors.secondary;
        }
        if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.colors.textPrimary) {
            colorBody.text = oldTheme.colors.textPrimary;
            colorBody.textPrimary = oldTheme.colors.textPrimary;
        }
        if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.colors.videoBackground) {
            colorBody.background = oldTheme.colors.videoBackground;
            colorBody.videoBackground = oldTheme.colors.videoBackground;
        }
        newTheme.colors = colorBody;
    }
    if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.dimensions) {
        newTheme.dimensions = oldTheme.dimensions;
    }
    if (oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.waitingRoom) {
        newTheme.waiting_room = {
            is_enabled: true,
            enable_preview: ((_a = oldTheme.waitingRoom) === null || _a === void 0 ? void 0 : _a.showSelf) !== undefined ? oldTheme.waitingRoom.showSelf : true,
            element: oldTheme.waitingRoom.roomElement,
        };
    }
    if (oldTheme.aloneHereElement !== undefined) {
        newTheme.alone_here = {
            is_enabled: oldTheme.aloneHereElement,
        };
    }
    newTheme.pip_mode = true;
    return newTheme;
};
exports.convertPresetToV2 = (exisitngPreset, preset) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
    // Conversion Works only for V1 presets
    const convertedPreset = Object.assign({}, exisitngPreset);
    if (preset.role) {
        const permissionBody = Object.assign({}, convertedPreset.permissions);
        if (((_a = preset.role.permissions) === null || _a === void 0 ? void 0 : _a.acceptWaitingRequests) !== undefined) {
            permissionBody.accept_waiting_requests = preset.role.permissions.acceptWaitingRequests;
        }
        if (((_b = preset.role.permissions) === null || _b === void 0 ? void 0 : _b.allowRecording) !== undefined) {
            permissionBody.can_record = preset.role.permissions.allowRecording;
        }
        if (((_c = preset.role.permissions) === null || _c === void 0 ? void 0 : _c.editDisplayName) !== undefined) {
            permissionBody.can_edit_display_name = preset.role.permissions.editDisplayName;
        }
        if (((_d = preset.role.permissions) === null || _d === void 0 ? void 0 : _d.disableParticipantAudio) !== undefined) {
            permissionBody.can_allow_participant_audio = preset.role.permissions.disableParticipantAudio;
        }
        if (((_e = preset.role.permissions) === null || _e === void 0 ? void 0 : _e.disableParticipantScreensharing) !== undefined) {
            permissionBody.can_allow_participant_screensharing = preset.role.permissions.disableParticipantScreensharing;
        }
        if (((_f = preset.role.permissions) === null || _f === void 0 ? void 0 : _f.disableParticipantVideo) !== undefined) {
            permissionBody.can_allow_participant_video = preset.role.permissions.disableParticipantVideo;
        }
        if (((_g = preset.role.permissions) === null || _g === void 0 ? void 0 : _g.kickParticipants) !== undefined) {
            permissionBody.kick_participant = preset.role.permissions.kickParticipants;
        }
        if (((_h = preset.role.permissions) === null || _h === void 0 ? void 0 : _h.pinParticipant) !== undefined) {
            permissionBody.pin_participant = preset.role.permissions.pinParticipant;
        }
        if ((_j = preset.role) === null || _j === void 0 ? void 0 : _j.plugins) {
            permissionBody.plugins = {
                can_start: preset.role.plugins.start,
                can_close: preset.role.plugins.close,
                can_edit_acl: preset.role.plugins.canEditAcl,
                config: {},
            };
            if ((_l = (_k = preset.role) === null || _k === void 0 ? void 0 : _k.pluginConfig) === null || _l === void 0 ? void 0 : _l.hostOnlyControl) {
                preset.role.pluginConfig.hostOnlyControl.forEach((pluginId) => {
                    permissionBody.plugins.config[pluginId] = {
                        defaultAccess: 'whitelist',
                    };
                });
            }
            if ((_o = (_m = preset.role) === null || _m === void 0 ? void 0 : _m.pluginConfig) === null || _o === void 0 ? void 0 : _o.handlesViewOnly) {
                preset.role.pluginConfig.hostOnlyControl.forEach((pluginId) => {
                    if (permissionBody.plugins.config[pluginId] === undefined) {
                        permissionBody.plugins.config[pluginId] = {
                            defaultAccess: 'blacklist',
                        };
                    }
                    permissionBody.plugins.config[pluginId].handlesViewOnly = true;
                });
            }
        }
        if ((_p = preset.role) === null || _p === void 0 ? void 0 : _p.produce) {
            permissionBody.produce = {
                video: Object.assign(Object.assign({}, permissionBody.produce.video), { allow: ((_q = preset.role.produce) === null || _q === void 0 ? void 0 : _q.video) !== undefined ? preset.role.produce.video : true }),
                audio: preset.role.produce.audio !== undefined ? preset.role.produce.audio : true,
                screenshare: Object.assign(Object.assign({}, permissionBody.produce.screenshare), { allow: ((_r = preset.role.produce) === null || _r === void 0 ? void 0 : _r.screenshare) !== undefined ? preset.role.produce.screenshare : true }),
            };
            permissionBody.polls = Object.assign(Object.assign({}, permissionBody.polls), { can_create: preset.role.produce.polls !== undefined ? preset.role.produce.polls : true });
            if ((_s = preset.role.produce) === null || _s === void 0 ? void 0 : _s.chat) {
                permissionBody.chat = Object.assign(Object.assign({}, permissionBody.chat), { public: Object.assign(Object.assign({}, preset.role.produce.chat), { can_send: true }) });
            }
        }
        if (preset.role.flags) {
            permissionBody.hidden_participant = preset.role.flags.hiddenParticipant;
            permissionBody.is_recorder = preset.role.flags.recorder;
        }
        permissionBody.show_participant_list = true;
        permissionBody.can_change_participant_role = false;
        permissionBody.view_type = preset.roomViewType ? types_1.DyteViewTypes[preset.roomViewType] : types_1.DyteViewTypes.groupCall;
        permissionBody.reactions = false;
        permissionBody.request_produce = !!(types_1.DyteViewTypes[preset.roomViewType] === types_1.DyteViewTypes.webinar && ((_u = (_t = preset.role) === null || _t === void 0 ? void 0 : _t.webinar) === null || _u === void 0 ? void 0 : _u.viewOnly));
        permissionBody.request_kick_participant = false;
        permissionBody.waiting_room_type = preset.role.waitingRoom ? types_1.WaitingRoomTypes[preset.role.waitingRoom] : types_1.WaitingRoomTypes.skip;
        permissionBody.accept_present_requests = !!(((types_1.DyteViewTypes[preset.roomViewType] === types_1.DyteViewTypes.webinar)
            && !((_w = (_v = preset.role) === null || _v === void 0 ? void 0 : _v.webinar) === null || _w === void 0 ? void 0 : _w.viewOnly)
            && ((_x = preset.role.permissions) === null || _x === void 0 ? void 0 : _x.acceptWaitingRequests)));
        // main modify
        convertedPreset.permissions = permissionBody;
    }
    if (preset.uiConfig) {
        convertedPreset.theme = exports.convertThemeToV2(convertedPreset.theme, preset.uiConfig);
    }
    return convertedPreset;
};
//# sourceMappingURL=presetConversion.js.map