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_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_hooks = require("../../hooks.js");
const require_download = require("../../utils/download.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/components/ecocup/cupList/hooks/useCupApi.ts
const getApiConfig = (isEcoShare) => {
	if (!isEcoShare) return {};
	return { isShopApi: true };
};
const useCupListsApi = (props) => {
	const { isEcoShare } = props;
	const [loading, setLoading] = (0, react.useState)(false);
	const [data, setData] = (0, react.useState)({});
	const { get } = require_hooks.useRequest();
	const { isNormalShop } = require_hooks.useGetGoalShopInfo();
	const isApp = require_hooks.useIsApp();
	const apiConfig = getApiConfig(isEcoShare);
	return {
		loading,
		data,
		run: (0, react.useCallback)(function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* (params) {
				try {
					const api = isApp ? "/h5/wallet/eco" : "/shop/wallet/eco";
					const isPartner = isNormalShop || (params === null || params === void 0 ? void 0 : params.partner_shop_id);
					setLoading(true);
					let _params = require_objectSpread2._objectSpread2({
						type: isPartner ? "partner" : "eco",
						with: [
							"customer:id,display_name",
							"rentShop:id,name",
							"returnShop:id,name",
							"currentShop:id,name"
						]
					}, params);
					if (isPartner) _params.with = [
						"customer:id,display_name",
						"rentShop:id,name",
						"returnShop:id,name"
					];
					if (isApp) _params = {
						num: params === null || params === void 0 ? void 0 : params.num,
						skip: params === null || params === void 0 ? void 0 : params.skip
					};
					const res = yield get(api, _params, apiConfig);
					setData((res === null || res === void 0 ? void 0 : res.data) || {});
					setLoading(false);
				} catch (error) {
					setLoading(false);
				}
			});
			return function(_x) {
				return _ref.apply(this, arguments);
			};
		}(), [])
	};
};
const useStatisticsApi = (props) => {
	const { isEcoShare } = props;
	const [data, setData] = (0, react.useState)({});
	const { get } = require_hooks.useRequest();
	const { isNormalShop } = require_hooks.useGetGoalShopInfo();
	const apiConfig = getApiConfig(isEcoShare);
	return {
		data,
		getStatics: (0, react.useCallback)(function() {
			var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (params) {
				try {
					const res = yield get("/shop/wallet/eco/statistics", require_objectSpread2._objectSpread2({ type: isNormalShop || (params === null || params === void 0 ? void 0 : params.partner_shop_id) ? "partner" : "eco" }, params), apiConfig);
					setData((res === null || res === void 0 ? void 0 : res.data) || {});
				} catch (error) {}
			});
			return function(_x2) {
				return _ref2.apply(this, arguments);
			};
		}(), [])
	};
};
const useEcoShopListsApi = (props) => {
	const { isEcoShare } = props;
	const [data, setData] = (0, react.useState)([]);
	const { get } = require_hooks.useRequest();
	const apiConfig = getApiConfig(isEcoShare);
	return {
		data,
		getEcoShopLists: (0, react.useCallback)(function() {
			var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (params) {
				try {
					const res = yield get("/shop/wallet/eco/shops", {
						skip: 1,
						num: 100
					}, apiConfig);
					setData((res === null || res === void 0 ? void 0 : res.data) || []);
				} catch (error) {}
			});
			return function(_x3) {
				return _ref3.apply(this, arguments);
			};
		}(), [])
	};
};
const useShareApi = () => {
	var _coreData$tenant3;
	const { post } = require_hooks.useRequest();
	const app = require_hooks.useGetApp();
	const context = require_useEngineContext.default();
	const coreData = (0, react.useMemo)(() => {
		var _app, _app$getStore, _app$getStore$getData;
		return app === null || app === void 0 || (_app = app()) === null || _app === void 0 || (_app = _app.data) === null || _app === void 0 || (_app = _app.store) === null || _app === void 0 || (_app$getStore = _app.getStore) === null || _app$getStore === void 0 || (_app$getStore = _app$getStore.call(_app)) === null || _app$getStore === void 0 || (_app$getStore$getData = _app$getStore.getDataByModel) === null || _app$getStore$getData === void 0 ? void 0 : _app$getStore$getData.call(_app$getStore, "tenantCore", "core");
	}, [app]);
	return { postShareApi: (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		try {
			var _app2, _app2$getStorage, _context$appHelper, _res$data, _coreData$tenant;
			const token = (_app2 = app()) === null || _app2 === void 0 || (_app2 = _app2.data) === null || _app2 === void 0 || (_app2 = _app2.storage) === null || _app2 === void 0 || (_app2$getStorage = _app2.getStorage) === null || _app2$getStorage === void 0 ? void 0 : _app2$getStorage.call(_app2, "token");
			const { copyToClipboard } = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.businessUtils) || {};
			if (!token) return;
			const res = yield post("/tenant/core/stash-token", {
				ttl: 10080 * 60,
				data: { login_token: token }
			}, { isPisell2: true });
			if ((res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.token) && (coreData === null || coreData === void 0 || (_coreData$tenant = coreData.tenant) === null || _coreData$tenant === void 0 ? void 0 : _coreData$tenant.default_domain)) {
				var _coreData$tenant2, _res$data2;
				const url = `${`https://${coreData === null || coreData === void 0 || (_coreData$tenant2 = coreData.tenant) === null || _coreData$tenant2 === void 0 ? void 0 : _coreData$tenant2.default_domain}/admin/ecocup/cup-list`}?source=ecoShare&key=${res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.token}`;
				copyToClipboard && copyToClipboard(url);
				(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.ecocup.toast.success-copy-link") });
			}
		} catch (error) {
			console.log("分享失败", error);
		}
	}), [coreData === null || coreData === void 0 || (_coreData$tenant3 = coreData.tenant) === null || _coreData$tenant3 === void 0 ? void 0 : _coreData$tenant3.default_domain]) };
};
const useExportApi = () => {
	const { post } = require_hooks.useRequest();
	const shopId = require_hooks.useGetShopId();
	const context = require_useEngineContext.default();
	return { postExportApi: (0, react.useCallback)(function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			try {
				var _context$appHelper2, _res$data3;
				const { downloadFile } = (context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.utils) || {};
				const { isPartner, listParams } = params;
				const res = yield post("/shop/wallet/eco/export-url", {
					type: isPartner ? "partner" : "eco",
					business_shop_id: !isPartner ? shopId : listParams === null || listParams === void 0 ? void 0 : listParams.partner_shop_id,
					operated_at_start: listParams === null || listParams === void 0 ? void 0 : listParams.operated_at_start,
					operated_at_end: listParams === null || listParams === void 0 ? void 0 : listParams.operated_at_end
				});
				if (res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 ? void 0 : _res$data3.export_url) {
					const filename = `shop-eco-cups-${(/* @__PURE__ */ new Date()).toISOString().replace(/[-:T.]/g, "").slice(0, 14)}.csv`;
					if (downloadFile) downloadFile(res.data.export_url, filename);
					else require_download.downloadFileUtils(res.data.export_url, filename);
					(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.ecocup.export.success") });
					return;
				}
				(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.ecocup.export.fail") });
			} catch (error) {
				(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.ecocup.export.fail") });
			}
		});
		return function(_x4) {
			return _ref4.apply(this, arguments);
		};
	}(), []) };
};
//#endregion
exports.useCupListsApi = useCupListsApi;
exports.useEcoShopListsApi = useEcoShopListsApi;
exports.useExportApi = useExportApi;
exports.useShareApi = useShareApi;
exports.useStatisticsApi = useStatisticsApi;
