import { VirtualService as J } from "../services/VirtualService.js";
import { useState as n, useEffect as u } from "react";
const x = ({
  virtualId: r,
  userName: s,
  virtualName: o,
  initAccessToken: i,
  onPromptError: U,
  onLoadError: c,
  metadata: t,
  onInitCompleted: m
}) => {
  const [p, f] = n(""), [V, l] = n(""), [h, S] = n([]), [e, g] = n();
  return u(() => {
    if (!(e && r === e.configs.virtualId && s === e.configs.userName && o === e.configs.virtualName && JSON.stringify(e.configs.metadata ?? {}) === JSON.stringify(t ?? {}))) {
      if (!r) {
        g(void 0);
        return;
      }
      g(
        new J({
          virtualId: r,
          userName: s,
          virtualName: o,
          initAccessToken: i,
          onPromptError: U,
          metadata: t,
          onInitCompleted: m
        })
      );
    }
  }, [
    r,
    s,
    o,
    t,
    e,
    i
  ]), u(() => {
    r && e && (f(""), l(""), S([]), e.initSession(r).then(() => {
      f(e.runnerUrl), l(e.modelUrl), S(e.cores);
    }).catch((y) => {
      c && c(y);
    }));
  }, [r, e]), {
    runnerUrl: p,
    modelUrl: V,
    virtualService: e,
    cores: h
  };
};
export {
  x as useVirtual
};
