function TMapGL(): Promise<void> {
	return new Promise<void>(function (resolve, reject) {
		const script = document.createElement('script');
		script.id = 'map-script';
		script.type = 'text/javascript';
		script.src =
			'https://map.qq.com/api/gljs?v=1.exp&libraries=tools&key=' +
			'JYTBZ-6GD3V-ZTZPJ-U35RX-EZMGQ-45BTF';
		script.onerror = () => reject();
		script.onload = () => resolve();
		const node = document.head.querySelector('#map-script');
		if (node !== null) {
			document.head.removeChild(node);
		}
		document.head.appendChild(script);
	});
}

const useTMap = () => {
	const getTMap = async (mapId: any) => {
		TMapGL().then(() => {
			// @ts-ignore
			let center = new window.TMap.LatLng(40.0402718, 116.2735831);
			// @ts-ignore
			mapId.current = new window.TMap.Map(
				document.querySelector('#map'),
				{
					center,
					zoom: 16.2,
					baseMap: [
						{ type: 'vector' }, //设置矢量底图
						// { type: 'traffic' }, //设置路况底图
					],
				},
			);
		});
	};

	return {
		getTMap,
	};
};

export default useTMap;
