import { DyteViewTypes, WaitingRoomConversionToV1Type, WaitingRoomTypes, } from '../types';
export const convertPresetToV1 = (exisitngPreset, preset) => {
    // Conversion Works only for V2 presets
    const convertedPreset = { ...exisitngPreset };
    if (preset.permissions) {
        const modifiedPermissions = { ...convertedPreset.role };
        if (preset.permissions.accept_waiting_requests !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                acceptWaitingRequests: preset.permissions.accept_waiting_requests,
            };
        }
        if (preset.permissions.can_allow_participant_audio !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                disableParticipantAudio: preset.permissions.can_allow_participant_audio,
            };
        }
        if (preset.permissions.can_allow_participant_screensharing !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                disableParticipantScreensharing: preset.permissions.can_allow_participant_screensharing,
            };
        }
        if (preset.permissions.can_allow_participant_video !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                disableParticipantVideo: preset.permissions.can_allow_participant_video,
            };
        }
        if (preset.permissions.kick_participant !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                kickParticipants: preset.permissions.kick_participant,
            };
        }
        if (preset.permissions.pin_participant !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                pinParticipant: preset.permissions.pin_participant,
            };
        }
        if (preset.permissions.can_record !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                allowRecording: preset.permissions.can_record,
            };
        }
        if (preset.permissions.can_edit_display_name !== undefined) {
            modifiedPermissions.permissions = {
                ...modifiedPermissions.permissions,
                editDisplayName: preset.permissions.can_edit_display_name,
            };
        }
        if (preset.permissions.waiting_room_type) {
            modifiedPermissions.waitingRoom = 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 = {
                ...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 = {
                ...modifiedPermissions.produce,
                polls: preset.permissions.polls.can_create,
            };
        }
        if (preset.permissions.chat) {
            modifiedPermissions.produce = {
                ...modifiedPermissions.produce,
                chat: {
                    text: preset.permissions.chat.public.text,
                    files: preset.permissions.chat.public.files,
                },
            };
        }
        if (preset.permissions.reactions !== undefined) {
            modifiedPermissions.produce = {
                ...modifiedPermissions.produce,
                reactions: preset.permissions.reactions,
            };
        }
        if (preset.permissions.hidden_participant !== undefined) {
            modifiedPermissions.flags = {
                ...modifiedPermissions.flags,
                hiddenParticipant: preset.permissions.hidden_participant,
            };
        }
        if (preset.permissions.is_recorder !== undefined) {
            modifiedPermissions.flags = {
                ...modifiedPermissions.flags,
                recorder: preset.permissions.is_recorder,
            };
        }
        if (preset.permissions.view_type === DyteViewTypes.webinar) {
            modifiedPermissions.webinar = {
                viewOnly: preset.permissions.request_produce,
            };
            convertedPreset.roomViewType = 'webinar';
        }
        if (preset.permissions.accept_present_requests !== undefined) {
            modifiedPermissions.webinar = {
                viewOnly: !preset.permissions.accept_present_requests,
                acceptWaitingRequests: preset.permissions.accept_present_requests,
            };
            convertedPreset.roomViewType = 'webinar';
        }
        convertedPreset["role"] = modifiedPermissions;
    }
    if (preset.theme) {
        const modifiedUiConfig = { ...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: preset.theme.control_bar.elements?.invite,
                screenShare: preset.theme.control_bar.elements?.screenshare,
                plugins: preset.theme.control_bar.elements?.plugins,
                participants: preset.theme.control_bar.elements?.participants,
                chat: preset.theme.control_bar.elements?.chat,
                polls: preset.theme.control_bar.elements?.polls,
                fullscreen: preset.theme.control_bar.elements?.fullscreen,
            };
        }
        if (preset.theme.header) {
            modifiedUiConfig["header"] = preset.theme.header.is_enabled;
            modifiedUiConfig["headerElements"] = {
                clock: preset.theme.header.elements?.timer,
                title: preset.theme.header.elements?.title,
                logo: !!(preset.theme.header.elements?.logo),
                participantCount: preset.theme.header.elements?.participant_count,
            };
            modifiedUiConfig["logo"] = preset.theme.header.elements?.logo;
        }
        if (preset.theme.auto_tune) {
            modifiedUiConfig.grid = { ...preset.theme.grid };
        }
        if (preset.theme.grid) {
            modifiedUiConfig.grid = { ...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;
};
export const convertThemeToV2 = (parentTheme, oldTheme) => {
    const newTheme = { ...parentTheme };
    const controlBarBody = {
        ...newTheme.control_bar,
        is_enabled: oldTheme.controlBar !== undefined ? oldTheme.controlBar
            : newTheme.control_bar.is_enabled,
    };
    const headerBody = {
        ...newTheme.header,
        is_enabled: oldTheme.header !== undefined ? oldTheme.header
            : newTheme.header.is_enabled,
    };
    if (oldTheme?.controlBarElements) {
        const elements = { ...controlBarBody.elements };
        if (oldTheme?.controlBarElements.chat !== undefined) {
            elements.chat = oldTheme.controlBarElements.chat;
        }
        if (oldTheme?.controlBarElements.fullscreen !== undefined) {
            elements.fullscreen = oldTheme.controlBarElements.fullscreen;
        }
        if (oldTheme?.controlBarElements.participants !== undefined) {
            elements.participants = oldTheme.controlBarElements.participants;
        }
        if (oldTheme?.controlBarElements.plugins !== undefined) {
            elements.plugins = oldTheme.controlBarElements.plugins;
        }
        if (oldTheme?.controlBarElements.polls !== undefined) {
            elements.polls = oldTheme.controlBarElements.polls;
        }
        if (oldTheme?.controlBarElements.screenShare !== undefined) {
            elements.screenshare = oldTheme.controlBarElements.screenShare;
        }
        if (oldTheme?.controlBarElements.share !== undefined) {
            elements.invite = oldTheme.controlBarElements.share;
        }
        elements.reactions = false;
        elements.layout = false;
        controlBarBody.elements = elements;
    }
    if (oldTheme?.headerElements) {
        const elements = { ...headerBody.elements };
        if (oldTheme?.headerElements.clock !== undefined) {
            elements.timer = oldTheme.headerElements.clock;
        }
        if (oldTheme?.headerElements.participantCount !== undefined) {
            elements.participant_count = oldTheme.headerElements.participantCount;
        }
        if (oldTheme?.headerElements.title !== undefined) {
            elements.title = oldTheme.headerElements.title;
        }
        elements.change_layout = false;
        headerBody.elements = elements;
    }
    if (oldTheme?.logo !== undefined && oldTheme.logo !== '') {
        headerBody.elements.logo = oldTheme.logo;
    }
    newTheme.header = headerBody;
    newTheme.control_bar = controlBarBody;
    if (oldTheme?.colors) {
        const colorBody = { ...newTheme.colors };
        if (oldTheme.colors.primary) {
            colorBody.primary = oldTheme.colors.primary;
        }
        if (oldTheme?.colors.secondary) {
            colorBody.secondary = oldTheme.colors.secondary;
        }
        if (oldTheme?.colors.textPrimary) {
            colorBody.text = oldTheme.colors.textPrimary;
            colorBody.textPrimary = oldTheme.colors.textPrimary;
        }
        if (oldTheme?.colors.videoBackground) {
            colorBody.background = oldTheme.colors.videoBackground;
            colorBody.videoBackground = oldTheme.colors.videoBackground;
        }
        newTheme.colors = colorBody;
    }
    if (oldTheme?.dimensions) {
        newTheme.dimensions = oldTheme.dimensions;
    }
    if (oldTheme?.waitingRoom) {
        newTheme.waiting_room = {
            is_enabled: true,
            enable_preview: oldTheme.waitingRoom?.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;
};
export const convertPresetToV2 = (exisitngPreset, preset) => {
    // Conversion Works only for V1 presets
    const convertedPreset = { ...exisitngPreset };
    if (preset.role) {
        const permissionBody = { ...convertedPreset.permissions };
        if (preset.role.permissions?.acceptWaitingRequests !== undefined) {
            permissionBody.accept_waiting_requests = preset.role.permissions.acceptWaitingRequests;
        }
        if (preset.role.permissions?.allowRecording !== undefined) {
            permissionBody.can_record = preset.role.permissions.allowRecording;
        }
        if (preset.role.permissions?.editDisplayName !== undefined) {
            permissionBody.can_edit_display_name = preset.role.permissions.editDisplayName;
        }
        if (preset.role.permissions?.disableParticipantAudio !== undefined) {
            permissionBody.can_allow_participant_audio = preset.role.permissions.disableParticipantAudio;
        }
        if (preset.role.permissions?.disableParticipantScreensharing !== undefined) {
            permissionBody.can_allow_participant_screensharing = preset.role.permissions.disableParticipantScreensharing;
        }
        if (preset.role.permissions?.disableParticipantVideo !== undefined) {
            permissionBody.can_allow_participant_video = preset.role.permissions.disableParticipantVideo;
        }
        if (preset.role.permissions?.kickParticipants !== undefined) {
            permissionBody.kick_participant = preset.role.permissions.kickParticipants;
        }
        if (preset.role.permissions?.pinParticipant !== undefined) {
            permissionBody.pin_participant = preset.role.permissions.pinParticipant;
        }
        if (preset.role?.plugins) {
            permissionBody.plugins = {
                can_start: preset.role.plugins.start,
                can_close: preset.role.plugins.close,
                can_edit_acl: preset.role.plugins.canEditAcl,
                config: {},
            };
            if (preset.role?.pluginConfig?.hostOnlyControl) {
                preset.role.pluginConfig.hostOnlyControl.forEach((pluginId) => {
                    permissionBody.plugins.config[pluginId] = {
                        defaultAccess: 'whitelist',
                    };
                });
            }
            if (preset.role?.pluginConfig?.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 (preset.role?.produce) {
            permissionBody.produce = {
                video: {
                    ...permissionBody.produce.video,
                    allow: preset.role.produce?.video !== undefined ? preset.role.produce.video : true,
                },
                audio: preset.role.produce.audio !== undefined ? preset.role.produce.audio : true,
                screenshare: {
                    ...permissionBody.produce.screenshare,
                    allow: preset.role.produce?.screenshare !== undefined ? preset.role.produce.screenshare : true,
                },
            };
            permissionBody.polls = {
                ...permissionBody.polls,
                can_create: preset.role.produce.polls !== undefined ? preset.role.produce.polls : true,
            };
            if (preset.role.produce?.chat) {
                permissionBody.chat = {
                    ...permissionBody.chat,
                    public: {
                        ...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 ? DyteViewTypes[preset.roomViewType] : DyteViewTypes.groupCall;
        permissionBody.reactions = false;
        permissionBody.request_produce = !!(DyteViewTypes[preset.roomViewType] === DyteViewTypes.webinar && preset.role?.webinar?.viewOnly);
        permissionBody.request_kick_participant = false;
        permissionBody.waiting_room_type = preset.role.waitingRoom ? WaitingRoomTypes[preset.role.waitingRoom] : WaitingRoomTypes.skip;
        permissionBody.accept_present_requests = !!(((DyteViewTypes[preset.roomViewType] === DyteViewTypes.webinar)
            && !preset.role?.webinar?.viewOnly
            && preset.role.permissions?.acceptWaitingRequests));
        // main modify
        convertedPreset.permissions = permissionBody;
    }
    if (preset.uiConfig) {
        convertedPreset.theme = convertThemeToV2(convertedPreset.theme, preset.uiConfig);
    }
    return convertedPreset;
};
//# sourceMappingURL=presetConversion.js.map