function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState } from 'react';
import useEngineContext, { usePlatform } from "../../hooks/useEngineContext";
import useLocale from "../../hooks/useLocale";
import scanListener from "./utils/scanListener";

/**
 * 获取当前语言
 */
export var useLanguage = function useLanguage() {
  var locale = useLocale();
  return locale || 'en';
};

/**
 * 扫码监听
 */
export var useScanListener = function useScanListener() {
  var _context$appHelper;
  var context = useEngineContext();
  var _ref = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {},
    interaction = _ref.interaction;
  var addListener = function addListener(key, callback) {
    scanListener.addListener(interaction, key, callback);
  };
  var removeListener = function removeListener(key) {
    scanListener.removeListener(key);
  };
  var setActiveListener = function setActiveListener(key) {
    scanListener.setActiveListener(key);
  };
  return {
    addListener: addListener,
    removeListener: removeListener,
    setActiveListener: setActiveListener
  };
};

/**
 * 获取app
 */
export var useGetApp = function useGetApp() {
  var _context$appHelper2;
  var context = useEngineContext();
  return (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.getApp;
};

/**
 * 获取modal
 */
export var useModal = function useModal(key) {
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    modalProps = _useState2[0],
    setModalProps = _useState2[1];
  var open = function open(props) {
    setModalProps(_objectSpread(_objectSpread({
      key: key
    }, props || {}), {}, {
      visible: true,
      onCancel: function onCancel() {
        var _props$onCancel;
        close();
        props === null || props === void 0 || (_props$onCancel = props.onCancel) === null || _props$onCancel === void 0 || _props$onCancel.call(props);
      },
      onOk: function onOk(values) {
        var _props$onOk;
        console.log("".concat(key, "_onOk"), values);
        close();
        props === null || props === void 0 || (_props$onOk = props.onOk) === null || _props$onOk === void 0 || _props$onOk.call(props, values);
      }
    }));
  };
  var close = function close() {
    setModalProps({
      visible: false
    });
  };
  return {
    modalProps: modalProps || {},
    visible: modalProps === null || modalProps === void 0 ? void 0 : modalProps.visible,
    open: open,
    close: close
  };
};
export var useRequest = function useRequest() {
  var _context$appHelper3;
  var context = useEngineContext();
  var request = (context === null || context === void 0 || (_context$appHelper3 = context.appHelper) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.utils) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.request) || {};
  var get = function get(url, data, config) {
    return request === null || request === void 0 ? void 0 : request.get(url, data, config);
  };
  var post = function post(url, data, config) {
    return request === null || request === void 0 ? void 0 : request.post(url, data, config);
  };
  var put = function put(url, data, config) {
    return request === null || request === void 0 ? void 0 : request.put(url, data, config);
  };
  var remove = function remove(url, data, config) {
    return request === null || request === void 0 ? void 0 : request.remove(url, data, config);
  };
  return {
    get: get,
    post: post,
    put: put,
    remove: remove
  };
};

/**
 * @description: 获取goal_shop_id和是否是普通店铺
 * @return {*}
 */
export var useGetGoalShopInfo = function useGetGoalShopInfo() {
  var _window;
  var context = useEngineContext();
  var search = ((_window = window) === null || _window === void 0 || (_window = _window.location) === null || _window === void 0 ? void 0 : _window.search) || {};
  var goalShopId = new URLSearchParams(search).get('goal_shop_id');
  return {
    goalShopId: goalShopId ? Number(goalShopId) : undefined,
    isNormalShop: !!goalShopId
  };
};

/**
 * 获取当前店铺id
 */
export var useGetShopId = function useGetShopId() {
  var _context$appHelper4, _utils$getStore, _utils$getStore$getSt, _utils$getData, _utils$store, _utils$store$getState;
  var context = useEngineContext();
  var utils = (context === null || context === void 0 || (_context$appHelper4 = context.appHelper) === null || _context$appHelper4 === void 0 ? void 0 : _context$appHelper4.utils) || {};
  var shopId = (utils === null || utils === void 0 || (_utils$getStore = utils.getStore) === null || _utils$getStore === void 0 || (_utils$getStore = _utils$getStore.call(utils)) === null || _utils$getStore === void 0 || (_utils$getStore$getSt = _utils$getStore.getState) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.call(_utils$getStore)) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.global) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.shop) === null || _utils$getStore$getSt === void 0 ? void 0 : _utils$getStore$getSt.id) || (utils === null || utils === void 0 || (_utils$getData = utils.getData) === null || _utils$getData === void 0 ? void 0 : _utils$getData.call(utils, 'shop_id')) || ((_utils$store = utils.store) === null || _utils$store === void 0 || (_utils$store$getState = _utils$store.getState) === null || _utils$store$getState === void 0 || (_utils$store$getState = _utils$store$getState.call(_utils$store)) === null || _utils$store$getState === void 0 || (_utils$store$getState = _utils$store$getState.global) === null || _utils$store$getState === void 0 || (_utils$store$getState = _utils$store$getState.globalConfig) === null || _utils$store$getState === void 0 ? void 0 : _utils$store$getState.id) || undefined;
  return shopId;
};

/**
 * @description: 是否是app端
 * @return {*}
 */
export var useIsApp = function useIsApp() {
  var platform = usePlatform();
  return platform === 'h5' || platform === 'pc';
};

/**
 * @description: 获取分享场景
 * @return {*}
 */
export var useGetShare = function useGetShare() {
  var _window2;
  var search = ((_window2 = window) === null || _window2 === void 0 || (_window2 = _window2.location) === null || _window2 === void 0 ? void 0 : _window2.search) || {};
  var source = new URLSearchParams(search).get('source');
  return {
    source: source,
    isEcoShare: source ? source === 'ecoShare' : false
  };
};