import { SHJDatasourceV2 as w } from "../index.mjs";
import { createMqttClient as y } from "../utils/mqtt.mjs";
import { DataSourceUtils as o } from "../utils/utils.mjs";
const x = async (t, a, n) => {
  try {
    const {
      protocol: r,
      endpoint: p,
      host: d,
      port: D,
      username: s,
      password: m,
      keepalive: q,
      clean: C,
      topic: e,
      qos: i
    } = t.source.mqtt;
    (await y(
      {
        uuid: t.id,
        protocol: r,
        endpoint: p,
        host: d,
        port: D,
        username: s,
        password: m,
        keepalive: q,
        clean: C,
        topic: e,
        qos: i
      },
      {
        onConnect: () => {
          n(o.noneData(t.id));
        },
        onError: (h) => {
          n(o.noneData(t.id));
        },
        onDisconnect: () => {
          n(o.noneData(t.id));
        },
        onReconnect: () => {
          n(o.noneData(t.id));
        },
        onClose: () => {
          n(o.noneData(t.id));
        }
      }
    )).subscribe(e, i, (h, S) => {
      w.processData(
        t.source,
        JSON.parse(S.toString()),
        t.id,
        a
      ).then((f) => {
        n(f);
      }).catch(() => {
        n(o.noneData(t.id));
      });
    });
  } catch {
    n(o.noneData(t.id));
  }
};
export {
  x as parseMqttData
};
