import { useEffect, useMemo, useState } from "react";
import { RICOS_VERSION } from "./../../ricos-schema/next-schema/index.js";
import { parseInstance } from "./../request-service-provider/parse-instance.js";
const useBiHelpers = ({
  instance,
  biSettings,
  msid = ""
}) => {
  const shouldLoadBi = !!instance && !!biSettings;
  const [state, setState] = useState({
    helpers: {},
    loading: shouldLoadBi
  });
  const { appDefId, metaSiteId: instanceMsid } = useMemo(
    () => instance ? parseInstance(instance) : { appDefId: "", metaSiteId: "" },
    [instance]
  );
  const metaSiteId = instanceMsid || msid;
  const [biLoadDeferred] = useState(() => {
    if (!shouldLoadBi) {
      return { promise: Promise.resolve({}), resolve: (_h) => {
      } };
    }
    let resolve;
    const promise = new Promise((r) => {
      resolve = r;
    });
    return { promise, resolve };
  });
  useEffect(() => {
    if (!shouldLoadBi || !biSettings) {
      setState({ helpers: {}, loading: false });
      return;
    }
    import("./index.js").then(
      ({ createBiHelpers }) => createBiHelpers({
        appDefId,
        metaSiteId,
        biSettings,
        wixRicosVersion: RICOS_VERSION,
        ricosVersion: RICOS_VERSION
      })
    ).then((result) => {
      setState({
        helpers: result.helpers,
        biLogger: result.biLogger,
        loading: false
      });
      biLoadDeferred.resolve(result.helpers);
    }).catch((error) => {
      console.error("[useBiHelpers] Failed to load BI helpers:", error);
      setState({ helpers: {}, loading: false });
      biLoadDeferred.resolve({});
    });
  }, [shouldLoadBi, appDefId, metaSiteId, biSettings, biLoadDeferred]);
  return { ...state, biLoadPromise: biLoadDeferred.promise };
};
export {
  useBiHelpers
};
//# sourceMappingURL=use-bi-helpers.js.map
