var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/ecocup/cupList/hooks/useCupApi.ts
var useCupApi_exports = {};
__export(useCupApi_exports, {
  useCupListsApi: () => useCupListsApi,
  useEcoShopListsApi: () => useEcoShopListsApi,
  useExportApi: () => useExportApi,
  useShareApi: () => useShareApi,
  useStatisticsApi: () => useStatisticsApi
});
module.exports = __toCommonJS(useCupApi_exports);
var import_react = require("react");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_hooks = require("../../hooks");
var getApiConfig = (isEcoShare) => {
  if (!isEcoShare) return {};
  return {
    isShopApi: true
  };
};
var useCupListsApi = (props) => {
  const { isEcoShare } = props;
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [data, setData] = (0, import_react.useState)({});
  const { get } = (0, import_hooks.useRequest)();
  const { isNormalShop } = (0, import_hooks.useGetGoalShopInfo)();
  const isApp = (0, import_hooks.useIsApp)();
  const apiConfig = getApiConfig(isEcoShare);
  const getCupLists = (0, import_react.useCallback)(async (params) => {
    try {
      const api = isApp ? "/h5/wallet/eco" : "/shop/wallet/eco";
      const isPartner = isNormalShop || (params == null ? void 0 : params.partner_shop_id);
      setLoading(true);
      let _params = {
        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 ? void 0 : params.num,
          skip: params == null ? void 0 : params.skip
        };
      }
      const res = await get(api, _params, apiConfig);
      setData((res == null ? void 0 : res.data) || {});
      setLoading(false);
    } catch (error) {
      setLoading(false);
    }
  }, []);
  return {
    loading,
    data,
    run: getCupLists
  };
};
var useStatisticsApi = (props) => {
  const { isEcoShare } = props;
  const [data, setData] = (0, import_react.useState)({});
  const { get } = (0, import_hooks.useRequest)();
  const { isNormalShop } = (0, import_hooks.useGetGoalShopInfo)();
  const apiConfig = getApiConfig(isEcoShare);
  const getStatics = (0, import_react.useCallback)(async (params) => {
    try {
      const isPartner = isNormalShop || (params == null ? void 0 : params.partner_shop_id);
      const _params = {
        type: isPartner ? "partner" : "eco",
        ...params
      };
      const res = await get("/shop/wallet/eco/statistics", _params, apiConfig);
      setData((res == null ? void 0 : res.data) || {});
    } catch (error) {
    }
  }, []);
  return {
    data,
    getStatics
  };
};
var useEcoShopListsApi = (props) => {
  const { isEcoShare } = props;
  const [data, setData] = (0, import_react.useState)([]);
  const { get } = (0, import_hooks.useRequest)();
  const apiConfig = getApiConfig(isEcoShare);
  const getEcoShopLists = (0, import_react.useCallback)(async (params) => {
    try {
      const _params = {
        skip: 1,
        num: 100
      };
      const res = await get("/shop/wallet/eco/shops", _params, apiConfig);
      setData((res == null ? void 0 : res.data) || []);
    } catch (error) {
    }
  }, []);
  return {
    data,
    getEcoShopLists
  };
};
var useShareApi = () => {
  var _a;
  const { post } = (0, import_hooks.useRequest)();
  const app = (0, import_hooks.useGetApp)();
  const context = (0, import_useEngineContext.default)();
  const coreData = (0, import_react.useMemo)(() => {
    var _a2, _b, _c, _d, _e, _f;
    return (_f = (_e = (_d = (_c = (_b = (_a2 = app == null ? void 0 : app()) == null ? void 0 : _a2.data) == null ? void 0 : _b.store) == null ? void 0 : _c.getStore) == null ? void 0 : _d.call(_c)) == null ? void 0 : _e.getDataByModel) == null ? void 0 : _f.call(_e, "tenantCore", "core");
  }, [app]);
  const postShareApi = (0, import_react.useCallback)(async () => {
    var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
    try {
      const token = (_d = (_c = (_b = (_a2 = app()) == null ? void 0 : _a2.data) == null ? void 0 : _b.storage) == null ? void 0 : _c.getStorage) == null ? void 0 : _d.call(_c, "token");
      const { copyToClipboard } = ((_f = (_e = context == null ? void 0 : context.appHelper) == null ? void 0 : _e.utils) == null ? void 0 : _f.businessUtils) || {};
      if (!token) return;
      const params = {
        // 有效期，7天，转成秒
        ttl: 7 * 24 * 60 * 60,
        data: {
          login_token: token
        }
      };
      const res = await post("/tenant/core/stash-token", params, {
        isPisell2: true
      });
      if (((_g = res == null ? void 0 : res.data) == null ? void 0 : _g.token) && ((_h = coreData == null ? void 0 : coreData.tenant) == null ? void 0 : _h.default_domain)) {
        const path = `https://${(_i = coreData == null ? void 0 : coreData.tenant) == null ? void 0 : _i.default_domain}/admin/ecocup/cup-list`;
        const url = `${path}?source=ecoShare&key=${(_j = res == null ? void 0 : res.data) == null ? void 0 : _j.token}`;
        copyToClipboard && copyToClipboard(url);
        (0, import_materials.PisellToast)({
          content: import_utils.locales.getText("pisell2.text.ecocup.toast.success-copy-link")
        });
      }
    } catch (error) {
      console.log("分享失败", error);
    }
  }, [(_a = coreData == null ? void 0 : coreData.tenant) == null ? void 0 : _a.default_domain]);
  return {
    postShareApi
  };
};
var useExportApi = () => {
  const { post } = (0, import_hooks.useRequest)();
  const shopId = (0, import_hooks.useGetShopId)();
  const context = (0, import_useEngineContext.default)();
  const postExportApi = (0, import_react.useCallback)(async (params) => {
    var _a, _b;
    try {
      const { downloadFile } = ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
      const { isPartner, listParams } = params;
      const _params = {
        type: isPartner ? "partner" : "eco",
        business_shop_id: !isPartner ? shopId : listParams == null ? void 0 : listParams.partner_shop_id,
        operated_at_start: listParams == null ? void 0 : listParams.operated_at_start,
        operated_at_end: listParams == null ? void 0 : listParams.operated_at_end
      };
      const res = await post("/shop/wallet/eco/export-url", _params);
      if ((_b = res == null ? void 0 : res.data) == null ? void 0 : _b.export_url) {
        const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:T.]/g, "").slice(0, 14);
        const filename = `shop-eco-cups-${timestamp}.csv`;
        downloadFile && downloadFile(res.data.export_url, filename);
        (0, import_materials.PisellToast)({
          content: import_utils.locales.getText("pisell2.text.ecocup.export.success")
        });
        return;
      }
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("pisell2.text.ecocup.export.fail")
      });
    } catch (error) {
      console.log("导出失败", error);
    }
  }, []);
  return {
    postExportApi
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useCupListsApi,
  useEcoShopListsApi,
  useExportApi,
  useShareApi,
  useStatisticsApi
});
