import store from "./store";
import { getSharePath } from "../../utils";

function convertMiniScheme(params) {
	const { appId, path, query, extraData } = params;
	let scheme = `alipays://platformapi/startapp?appId=${appId}`;
	if (path) {
		scheme += `&page=${encodeURIComponent(path)}`;
	}
	if (query) {
		const newQuery = Object.keys(query)
			.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(query[k])}`)
			.join("&");
		scheme += `&query=${encodeURIComponent(newQuery)}`;
	}
	if (extraData && Object.keys(extraData).length) {
		console.log("! extraData 不支持在 scheme 中使用", params);
	}
	return scheme;
}

Page(
	store.register({
		async onLoad(options) {
			this.options = options;
			const { pageUuid } = options;
			this.pageUuid = pageUuid;
			await this.dispatch("init", this.options);
		},
		async onResume() {
			this.dispatch("onResume");
		},
		async onReloadHandle() {
			await this.dispatch("init", {
				/*
				 * 刷新关闭页面数据缓存
				 * */
				noCache: true,
				pageUuid: this.pageUuid,
				isPullRefresh: true,
			});
		},
		onReachBottom() {},
		onPageScroll() {},
		onShow() {},
		/*
		 * 增加下拉刷新
		 * */
		async onPullDownRefresh() {
			await this.onReloadHandle();
			my.stopPullDownRefresh();
		},
		onPullIntercept() {},
		onUnload() {},
		onShareAppMessage() {
			return {
				title: this.data.pageName || "",
				path: getSharePath(this.options),
			};
		},
	})
);
