import { getStorageData } from '@/storage/';
import { DeviceEventEmitter } from 'react-native';

interface Message {
	info: any;
	data: any;
}
class MessageCenter {
	async send(message: Message) {
		console.log('MessageCenter', message);
		const { target, messageId, actionType } = message.info;
		switch (message.info.actionType) {
			case 'IFRAME_GET_EXTENSION_STORGE_DATA':
				const _data = await getStorageData(message.data.key);
				target.postMessage(
					JSON.stringify({
						info: { messageId, actionType },
						data: _data,
					}),
				);
				break;
			case 'IFRAME_TWITTER_API_DO_TASK':
				DeviceEventEmitter.emit(actionType, message.data);
				break;
			default:
				break;
		}
	}
}

const messageCenter = new MessageCenter();
export default messageCenter;
