// 缓存非组件相关信息的
if (!global.__biliCacheData) global.__biliCacheData = {
	font: {                      // 处理字体相关的参数
		openGlobal: false           // 是否开启全局字体
	},
	getCurrentPageId: ()=>{},    // 标准获取pageId方法，保证正确性
	pageIdStatus: '',            // 缓存生命周期变化中pageId的过渡状态
	eventHandler: {				 // 缓存小程序所有需要缓存handler
		keyboard: {
			_cache: {}
		}
	},
	api: {
		keyboard: {
			current: '',
		}
	},
	currentPageId: '',            // 缓存生命周期变化中的pageId（非标准，记录过渡状态，标准请使用 getCurrentPageId 方法）
	// 获取客户端 js bind 提供的统一能力。
	getObjectForNativeCanvas: function (funName) {
		return global.BLGlobal[funName].bind(global.BLGlobal);
	},
	canvas: {
		Image:function (width, height) {
			let _canvasForNA = global.__biliComponentData.canvas.canvasForNA();
			let _image = _canvasForNA.createImage();
			_image.width = width;
			_image.height = height;
			return _image;
		},
		ImageData:function () {
			let _temp = Array.prototype.slice.apply(arguments);
			let _canvasForNA = global.__biliComponentData.canvas.canvasForNA();
			let _offscreenCanvas = _canvasForNA.createCanvas(true, global.__biliCacheData.getCurrentPageId() ,2);
			let _ctx = _offscreenCanvas.getContext('2d');
			return _ctx.createImageData(..._temp);
		}
	},
	eventHandlers: {
		// 截屏回调
		captureListeningHandlers: [],
		// 陀螺仪回调
		gyroscopeChangeHandlers: {}
	},
	smallAppInfo: {
		appid: APPID
	}
};

// 缓存组件相关信息的
if (!global.__biliComponentData) global.__biliComponentData = {
	canvas: {
		canvasForNA: ()=>{},
		waitingCanvas: [],
		checkoutQuery: ()=>{
			// console.log('>>> 缓存check...')
			let _temp = global.__biliComponentData.canvas.waitingCanvas;
			let _pageId = global.__biliCacheData.getCurrentPageId();
			let _delIndex = []
			if (_temp.length > 0) {
				_temp.forEach((_x, i) => {
					let __temp = _x.params.filter(x=> x && x['getCanvasInstance']);
					let _isReadyByAll = true;
					if (__temp.length > 0) {
						__temp.forEach(_x => {
							let _readyForCanvas = global.canvas &&
								global.canvas[_pageId] &&
								global.canvas[_pageId][_x["getCanvasInstance"]] &&
								global.canvas[_pageId][_x["getCanvasInstance"]].status === 'ready';
							_isReadyByAll = _isReadyByAll && _readyForCanvas
						});
					}
					if (_isReadyByAll) {
						_x.params.forEach(_x => {
							if (_x["getCanvasInstance"]) {
								// canvas版本支持
								if (bl.isCanvasSupportedSync()) {
									// console.log('>>> checkout ', _isReadyByAll, global.canvas[_pageId][_x["getCanvasInstance"]].canvas)
									_x['node'] = global.canvas[_pageId][_x["getCanvasInstance"]].canvas;
									// 特殊兼容历史版本...
									global.canvas[_pageId][_x["getCanvasInstance"]].canvas.createImage = global.__biliCacheData.canvas.Image;
									global.canvas[_pageId][_x["getCanvasInstance"]].canvas.createImageData = global.__biliCacheData.canvas.ImageData;
								} else {
									_x['node'] = null
								}
								// 删除标记
								delete _x["getCanvasInstance"];
							}
						});
						// 调用
						_x.callback(_x.params);
						if (Service.callbacks[_x.callbackId]) delete Service.callbacks[_x.callbackId];
						_x.queueCb.forEach((item, index) => {
							if (item) {
								item(_x.params[index]);
							}
						});
						// 记录删除的索引
						_delIndex.push(i);
					}
				})
			}
			if (_delIndex.length > 0) {
				_delIndex.forEach(y=>{
					_temp.splice(y,1)
				})
			}
		}
	}
};

// 构建相关信息
global.$buildInfo = {
	buildTime: SAPP_BUILDTIME,
	version: SAPP_VERSION,
	smallApp: SAPP_SMALLAPP,
	baseVersion: global._bili_runtime? global._bili_runtime.version : '',
	baseTime: global._bili_runtime? global._bili_runtime.data : ''
}

// 加载字体用的空方法，为安卓老版本兜底使用
Service.executeJSOnPreLoadRender = function () { return ''; }
