import React, { useContext, useState, } from 'react';
import { PluginServiceContext } from '../services/PluginService';
import { PeersServiceContext } from '../services/PeersService';
import { Flex, Box, Button, Icon, } from '../themes/components';
import { WebView } from 'react-native-webview';
import { View } from 'react-native';
class BasicEvents {
    constructor() {
        this.fxs = [];
    }
    onMessage(data) { this.fxs.forEach((fx) => fx(data)); }
    addEventListener(fx) { this.fxs.push(fx); }
}
const EM = new BasicEvents();
export const PluginWrapper = ({ frame, height, width, }) => {
    const service = useContext(PluginServiceContext);
    const { selfPeer } = useContext(PeersServiceContext);
    const removePlugin = () => {
        service.pluginManager.removeRoomPlugin({
            id: frame.plugin,
        });
    };
    const [loading, setLoading] = useState(true);
    const registerIframe = (element) => {
        service.pluginManager.addIframeView(frame, element, EM);
    };
    return (<Flex height={height} width={width}>
			<Flex direction="column">
				{selfPeer.isHost && (<Flex justify="flex-end" align="center" cursor="pointer" bg="secondary.700">
						<Button onClick={removePlugin}>
							<Box p={4}>
								<Icon name="close" boxSize="16"/>
							</Box>
						</Button>
					</Flex>)}
				<Flex width={width} height={height} justify='center' align="center" direction="row">
					
			
						<View style={{
        transform: [{ rotate: '90deg' }],
        width: height * 0.92,
        height: width * 0.95,
    }}>
							<WebView source={{ uri: frame.url }} ref={registerIframe} onMessage={(event) => {
        const data = JSON.parse(event.nativeEvent.data);
        if (data.type) {
            EM.onMessage(data);
        }
    }} injectedJavaScript={`
								setTimeout(function(){
									window.parent = {};
									function postMessage (a, b) {
										if (!window.ReactNativeWebView.postMessage) {
											setTimeout(() => {k
												postMessage(a, b);
											}, 100);
											return;
										}
										window.ReactNativeWebView.postMessage(JSON.stringify(a));
									}
									window.parent.postMessage = (a, b) => {
										postMessage(a);
									}
								})`}/>
						</View>

				</Flex>
			</Flex>
		</Flex>);
};
export default PluginWrapper;
//# sourceMappingURL=PluginWrapper.js.map