const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../../../table/utils.js");
let _pisell_utils = require("@pisell/utils");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let vod_js_sdk_v6 = require("vod-js-sdk-v6");
vod_js_sdk_v6 = require_runtime.__toESM(vod_js_sdk_v6);
//#region src/components/dataSourceComponents/fields/Upload/shopUploadServe.ts
const filetypes = {
	"vnd.openxmlformats-officedocument.wordprocessingml.document": {
		icon: "iconword",
		type: "applicatio",
		format: "docx"
	},
	"vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
		icon: "iconxlsx",
		type: "applicatio",
		format: "xlsx"
	},
	csv: {
		icon: "icontxt",
		type: "text",
		format: "csv"
	},
	"vnd.ms-excel": {
		icon: "iconxlsx",
		type: "applicatio",
		format: "xls"
	},
	msword: {
		icon: "iconword",
		type: "applicatio",
		format: "doc"
	},
	pdf: {
		icon: "iconpdf",
		type: "applicatio",
		format: "pdf"
	}
};
/**
* @Title: 获取 阿里云 Signature
* @Describe:
* @Author: Wzw
*/
const getPolicy = () => require_utils.request.getRequest().get("/shop/ali-upload/policy", { type: "image" });
/**
* @Title: 获取图片尺寸
* @Describe:
* @Author: Wzw
* @param {*} async
*/
const getImageWidth = function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* (file) {
		return new Promise((resolve, reject) => {
			try {
				let url = window.URL || window.webkitURL;
				let img = new Image();
				img.src = url.createObjectURL(file);
				img.onload = function() {
					resolve({
						width: img.width,
						height: img.height
					});
				};
				img.onerror = function() {
					resolve({
						width: 0,
						height: 0
					});
				};
			} catch (err) {
				resolve({
					width: 0,
					height: 0
				});
			}
		});
	});
	return function getImageWidth(_x) {
		return _ref.apply(this, arguments);
	};
}();
/**
* @Title: 重新生成文件名
* @Describe:
* @Author: Wzw
* @param {string} file
*/
const formatFileName = function() {
	var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (file) {
		var _filetypes$_suffix;
		const imgSize = yield getImageWidth(file);
		let _suffix = ((file === null || file === void 0 ? void 0 : file.type) || "image/png").split("/")[1];
		let fileType = (_filetypes$_suffix = filetypes[_suffix]) === null || _filetypes$_suffix === void 0 ? void 0 : _filetypes$_suffix.format;
		if (fileType) _suffix = fileType;
		return (0, dayjs.default)().format("YYYY_MM_DD_HH_mm_ss_SSS") + "_" + (0, _pisell_utils.getUniqueId)() + "__" + imgSize.width + "_" + imgSize.height + "__." + _suffix;
	});
	return function formatFileName(_x2) {
		return _ref2.apply(this, arguments);
	};
}();
const uploadFileAli = function() {
	var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (file) {
		const policyData = yield getPolicy();
		let _name = yield formatFileName(file);
		let formData = new FormData();
		formData.append("Content-Type", "multipart/form-data");
		formData.append("OSSAccessKeyId", policyData.data.access_key_id);
		formData.append("policy", policyData.data.policy);
		formData.append("Signature", policyData.data.signature);
		formData.append("key", policyData.data.dir + _name);
		formData.append("success_action_status", "200");
		formData.append("callback", policyData.data.callback);
		formData.append("file", file);
		return require_utils.request.getRequest().post(policyData.data.cdn_domain, formData);
	});
	return function uploadFileAli(_x3) {
		return _ref3.apply(this, arguments);
	};
}();
const uploadAli = function() {
	var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (file) {
		return uploadFileAli(file);
	});
	return function uploadAli(_x4) {
		return _ref4.apply(this, arguments);
	};
}();
const getObjectURL = (file) => {
	var url = null;
	if (window.createObjectURL != void 0) url = window.createObjectURL(file);
	else if (window.URL != void 0) url = window.URL.createObjectURL(file);
	else if (window.webkitURL != void 0) url = window.webkitURL.createObjectURL(file);
	return url;
};
/**
* @Title: base64转file
* @Describe:
* @Author: Wzw
* @param {any} dataurl
* @param {string} filename
*/
const dataURLtoFile = (dataurl, filename) => {
	var arr = dataurl.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
	while (n--) u8arr[n] = bstr.charCodeAt(n);
	return new File([u8arr], filename, { type: mime });
};
/**
* @Title: 获取视频封面
* @Describe:
* @Author: Wzw
* @param {any} file
*/
const getVideoCover = (file) => {
	const url = getObjectURL(file);
	return new Promise(function(resolve, reject) {
		try {
			let _video = document.createElement("video");
			_video.setAttribute("crossOrigin", "anonymous");
			_video.setAttribute("preload", "auto");
			_video.muted = true;
			_video.setAttribute("autoplay", "autoplay");
			_video.src = url;
			_video.addEventListener("loadeddata", function() {
				this.currentTime = 1;
			});
			_video.addEventListener("seeked", function() {
				this.width = this.videoWidth;
				this.height = this.videoHeight;
				if (this.width === 0 || this.height === 0) return resolve("");
				let canvas = document.createElement("canvas");
				let ctx = canvas.getContext("2d");
				canvas.width = this.width;
				canvas.height = this.height;
				if (ctx === null) return;
				ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
				resolve(dataURLtoFile(canvas.toDataURL("image/jpeg", 1), file.name.split(".")[0] + "_cover.jpeg"));
			});
		} catch (err) {
			reject();
		}
	});
};
/**
* @Title: 获取腾讯云 Signature
* @Describe:
* @Author: Wzw
*/
const getSignature = () => require_utils.request.getRequest().get("/shop/core/tx-video-sign").then((res) => {
	var _res$data;
	return res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.sign;
});
/**
* @Title: 上传到腾讯云
* @Describe:
* @Author: Wzw
* @param {any} file
*/
const uploadFileTencent = function() {
	var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (file) {
		return new Promise(function() {
			var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (resolve, reject) {
				try {
					const coverFile = yield getVideoCover(file);
					console.log(coverFile);
					const coverData = yield uploadFileAli(coverFile);
					const uploader = new vod_js_sdk_v6.default({ getSignature }).upload({
						mediaFile: file,
						mediaName: file.name
					});
					uploader.on("media_upload", (info) => {});
					uploader.done().then((doneResult) => {
						var _coverData$data, _doneResult$video;
						return resolve({
							status: true,
							data: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, doneResult), {}, {
								cover: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, doneResult === null || doneResult === void 0 ? void 0 : doneResult.cover), {}, { url: coverData === null || coverData === void 0 || (_coverData$data = coverData.data) === null || _coverData$data === void 0 ? void 0 : _coverData$data.url }),
								url: doneResult === null || doneResult === void 0 || (_doneResult$video = doneResult.video) === null || _doneResult$video === void 0 ? void 0 : _doneResult$video.url
							})
						});
					}).catch((err) => {
						return reject(err);
					});
				} catch (err) {
					return reject(err);
				}
			});
			return function(_x5, _x6) {
				return _ref5.apply(this, arguments);
			};
		}());
	});
	return function uploadFileTencent(_x7) {
		return _ref6.apply(this, arguments);
	};
}();
/**
* @Title: 上传至腾讯云
* @Describe:
* @Author: Wzw
* @param {*} async
*/
const uploadTencent = function() {
	var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (file) {
		return yield uploadFileTencent(file);
	});
	return function uploadTencent(_x8) {
		return _ref7.apply(this, arguments);
	};
}();
const uploadFile = (file) => {
	if (file.type.startsWith("image")) return uploadAli(file);
	else if (file.type.startsWith("video")) return uploadTencent(file);
	else return uploadAli(file);
};
//#endregion
exports.uploadFile = uploadFile;
