var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasDevice/appDevicePlanning/hooks/useDeviceLinking.ts
var useDeviceLinking_exports = {};
__export(useDeviceLinking_exports, {
  useDeviceLinking: () => useDeviceLinking
});
module.exports = __toCommonJS(useDeviceLinking_exports);
var import_react = require("react");
var import_api = require("../api");
var import_model = require("../model");
var useDeviceLinking = ({
  utils,
  onLinked,
  reloadDevices,
  setStoredIotDevice,
  setTakeoverDevice,
  setInitCheckTakeoverDevice
}) => {
  const [linkingDeviceId, setLinkingDeviceId] = (0, import_react.useState)();
  const handleLinkDevice = async (record) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
    const app = (_a = utils == null ? void 0 : utils.getApp) == null ? void 0 : _a.call(utils);
    setLinkingDeviceId(record.id);
    try {
      const language = ((_b = app == null ? void 0 : app.locales) == null ? void 0 : _b.getLocale()) || "en";
      const deviceDetail = await ((_e = (_d = (_c = app == null ? void 0 : app.plugins) == null ? void 0 : _c.get("device")) == null ? void 0 : _d.getDeviceDetail) == null ? void 0 : _e.call(_d, { language }));
      const linkedDevice = await (0, import_api.linkDevice)(
        record.id,
        (deviceDetail == null ? void 0 : deviceDetail.data) || {}
      );
      const linkedDeviceSimpleInfo = {
        id: linkedDevice == null ? void 0 : linkedDevice.id,
        // 设备id
        number: linkedDevice == null ? void 0 : linkedDevice.number
        // iot设备编号
      };
      (_f = app == null ? void 0 : app.storage) == null ? void 0 : _f.setStorage(
        "iotDevice",
        JSON.stringify(linkedDeviceSimpleInfo || {})
      );
      (_g = app == null ? void 0 : app.storage) == null ? void 0 : _g.setStorage(
        "iotDeviceLastUsed",
        JSON.stringify(linkedDeviceSimpleInfo || {})
      );
      try {
        await ((_j = (_i = (_h = app == null ? void 0 : app.plugins) == null ? void 0 : _h.get("nativeStorage")) == null ? void 0 : _i.set) == null ? void 0 : _j.call(_i, { key: "iotDeviceFullInfo", value: linkedDevice || {} }));
      } catch (error) {
        (_k = app == null ? void 0 : app.logger) == null ? void 0 : _k.addLog({
          type: "error",
          title: "将完整信息存入 nativeStorage 失败",
          metadata: { error }
        });
      }
      setStoredIotDevice({
        id: typeof (linkedDevice == null ? void 0 : linkedDevice.id) === "number" ? linkedDevice.id : void 0,
        number: (linkedDevice == null ? void 0 : linkedDevice.number) ? String(linkedDevice.number) : ""
      });
      (0, import_model.persistDeviceModel)(record.typeCode, record.modelCode);
      const setting = await (0, import_api.getDeviceSetting)(record.id, ["general.channel"]);
      const channel = (setting == null ? void 0 : setting["general.channel"]) ? String(setting["general.channel"]) : "system";
      (_l = localStorage == null ? void 0 : localStorage.setItem) == null ? void 0 : _l.call(localStorage, "pisell_app_channel", channel);
      try {
        await ((_o = (_n = (_m = app == null ? void 0 : app.plugins) == null ? void 0 : _m.get("nativeStorage")) == null ? void 0 : _n.set) == null ? void 0 : _o.call(_n, { key: "pisell_app_channel", value: channel }));
      } catch (error) {
        (_p = app == null ? void 0 : app.logger) == null ? void 0 : _p.addLog({
          type: "error",
          title: "将渠道信息存入 nativeStorage 失败",
          metadata: { error }
        });
      }
      setTakeoverDevice(void 0);
      setInitCheckTakeoverDevice(void 0);
      reloadDevices();
      await (onLinked == null ? void 0 : onLinked({ device: linkedDevice, channel }));
    } catch (e) {
    } finally {
      setLinkingDeviceId(void 0);
    }
  };
  return {
    linkingDeviceId,
    handleLinkDevice
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDeviceLinking
});
