import { useAppStore } from "@/store/useAppStore";

// 复制文本
export const copyText = ({ text }) => {
	if (!text) { return }

	uni.setClipboardData({
		data: text,
		success() {
			uni.showToast({
				title: 'Success!',
				icon: 'success',
				mask: true,
				duration: 1500
			})
		}
	})
}

// 调用系统分享 仅限https
/**
 * 
 * @param {{title?:string, content?:string, url: URL|string}} shareMsg 
 * @returns 
 */
export const shareWithSystem = (shareMsg) => {
	if (!shareMsg) return
	const appStore = useAppStore();

	// #ifdef APP-PLUS
	plus.share.sendWithSystem({
		type: 'text',
		content: shareMsg?.content ?? '',
		href: shareMsg.url,
	}, () => {
		uni.showToast({
			title: 'Success!',
			icon: 'success',
			mask: true,
			duration: 1500
		})
	}, err => {
		uni.setClipboardData({
			data: shareMsg.url,
			complete: () => {
				uni.hideToast()
			}
		})

	})
	// #endif
	// #ifdef H5
	if (window !== window.parent) {
		window.parent.postMessage({ type: 'share', data: { title: shareMsg?.title ?? '', text: shareMsg?.content ?? '', url: shareMsg.url } }, '*')
	} else {
		if (window.navigator.share) {
			window.navigator.share({ title: shareMsg?.title??'', text: shareMsg?.content??'', url: shareMsg.url })
		}
	}
	if (appStore.isCoat) {
		// NOTE: for 殼包
		const msg = {
			type: 'share',
			url: shareMsg.url
		};
		parent.postMessage(JSON.stringify(msg), '*');
	} else {
		uni.setClipboardData({
			data: shareMsg.url
		})
		uni.hideToast()
	}
	// #endif
}
