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/eftpos/hooks.tsx
var hooks_exports = {};
__export(hooks_exports, {
  useCheckPairRequest: () => useCheckPairRequest,
  usePairRequest: () => usePairRequest,
  usePairTerminalo: () => usePairTerminalo,
  useRequest: () => useRequest,
  useTyro: () => useTyro
});
module.exports = __toCommonJS(hooks_exports);
var import_react = require("react");
var import_load = __toESM(require("../../utils/load"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var usePairTerminalo = (type, params) => {
  const ref = (0, import_react.useRef)();
  const iclientRef = (0, import_react.useRef)();
  const url = (0, import_react.useMemo)(() => {
    return params && params.url && params.version ? `${params.url}/${params.version}` : "https://iclientsimulator.test.tyro.com/iclient-v1.js";
  }, [params]);
  const loadSDK = () => {
    if (window == null ? void 0 : window.TYRO) {
      return Promise.resolve(window == null ? void 0 : window.TYRO);
    }
    return (0, import_load.default)(url, document.head).then(() => {
      console.log(window == null ? void 0 : window.TYRO);
      return window == null ? void 0 : window.TYRO;
    });
  };
  const pair = async ({
    mid,
    tid,
    apiKey,
    posProductData
  }, cb) => {
    if (type !== "tyro") {
      cb({});
      return;
    }
    if (!ref.current) {
      ref.current = await loadSDK();
    }
    if (!ref.current) {
      cb({});
      return;
    }
    if (!iclientRef.current) {
      const TYRO = ref.current;
      iclientRef.current = new TYRO.IClient(apiKey, posProductData);
    }
    iclientRef.current.pairTerminal(mid, tid, (response) => {
      console.log(response);
      cb(response);
    });
  };
  return pair;
};
var useTyro = (params) => {
  const ref = (0, import_react.useRef)();
  const [load, setLoad] = (0, import_react.useState)(false);
  const url = (0, import_react.useMemo)(() => {
    return params && params.url && params.version ? `${params.url}/${params.version}` : "https://iclientsimulator.test.tyro.com/iclient-v1.js";
  }, [params]);
  (0, import_react.useEffect)(() => {
    (0, import_load.default)(url, document.head).then(() => {
      return window == null ? void 0 : window.TYRO;
    }).then(
      (res) => {
        setLoad(true);
        ref.current = res;
      },
      () => {
        setLoad(true);
      }
    );
  }, [url]);
  return [ref, load];
};
var useRequest = () => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  return ((_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.request) || {};
};
var usePairRequest = () => {
  const request = useRequest();
  const run = (params) => {
    return request.post("/shop/pay/card-reader/pair", params, {});
  };
  return {
    run
  };
};
var useCheckPairRequest = () => {
  const request = useRequest();
  const run = (params) => {
    return request.post("/shop/pay/card-reader/pairing-info", params, { customResponse: true });
  };
  return {
    run
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useCheckPairRequest,
  usePairRequest,
  usePairTerminalo,
  useRequest,
  useTyro
});
