import { jsx as q } from "react/jsx-runtime";
import z from "./index-lottie_light.js";
import { useState as B, useRef as C, useEffect as u } from "react";
const U = (d, F) => {
  const {
    animationData: i,
    loop: w,
    autoplay: m,
    initialSegment: o,
    onComplete: l,
    onLoopComplete: f,
    onEnterFrame: p,
    onSegmentStart: g,
    onConfigReady: y,
    onDataReady: S,
    onDataFailed: h,
    onLoadedImages: D,
    onDOMLoaded: L,
    onDestroy: A,
    // Specified here to take them out from the 'rest'
    lottieRef: G,
    renderer: H,
    name: K,
    assetsPath: Q,
    rendererSettings: W,
    // TODO: find a better way to extract the html props to avoid specifying
    //  all the props that we want to exclude (as you can see above)
    ...E
  } = d, [v, O] = B(!1), n = C(null), c = C(null), T = () => {
    var e;
    (e = n.current) == null || e.play();
  }, _ = () => {
    var e;
    (e = n.current) == null || e.stop();
  }, I = () => {
    var e;
    (e = n.current) == null || e.pause();
  }, P = (e) => {
    var t;
    (t = n.current) == null || t.setSpeed(e);
  }, b = (e, t) => {
    var r;
    (r = n.current) == null || r.goToAndPlay(e, t);
  }, x = (e, t) => {
    var r;
    (r = n.current) == null || r.goToAndStop(e, t);
  }, J = (e) => {
    var t;
    (t = n.current) == null || t.setDirection(e);
  }, M = (e, t) => {
    var r;
    (r = n.current) == null || r.playSegments(e, t);
  }, N = (e) => {
    var t;
    (t = n.current) == null || t.setSubframe(e);
  }, V = (e) => {
    var t;
    return (t = n.current) == null ? void 0 : t.getDuration(e);
  }, j = () => {
    var e;
    (e = n.current) == null || e.destroy(), n.current = void 0;
  }, k = (e = {}) => {
    var a;
    if (!c.current)
      return;
    (a = n.current) == null || a.destroy();
    const t = i && JSON.parse(JSON.stringify(i)), r = {
      ...d,
      ...e,
      animationData: t,
      container: c.current
    };
    return n.current = z.loadAnimation(r), O(!!n.current), () => {
      var s;
      (s = n.current) == null || s.destroy(), n.current = void 0;
    };
  };
  return u(() => {
    const e = k();
    return () => e == null ? void 0 : e();
  }, [i, w]), u(() => {
    n.current && (n.current.autoplay = !!m);
  }, [m]), u(() => {
    if (n.current) {
      if (!o) {
        n.current.resetSegments(!0);
        return;
      }
      !Array.isArray(o) || !o.length || ((n.current.currentRawFrame < o[0] || n.current.currentRawFrame > o[1]) && (n.current.currentRawFrame = o[0]), n.current.setSegment(o[0], o[1]));
    }
  }, [o]), u(() => {
    const t = [{
      name: "complete",
      handler: l
    }, {
      name: "loopComplete",
      handler: f
    }, {
      name: "enterFrame",
      handler: p
    }, {
      name: "segmentStart",
      handler: g
    }, {
      name: "config_ready",
      handler: y
    }, {
      name: "data_ready",
      handler: S
    }, {
      name: "data_failed",
      handler: h
    }, {
      name: "loaded_images",
      handler: D
    }, {
      name: "DOMLoaded",
      handler: L
    }, {
      name: "destroy",
      handler: A
    }].filter((a) => a.handler != null);
    if (!t.length)
      return;
    const r = t.map(
      /**
       * Handle the process of adding an event listener
       * @param {Listener} listener
       * @return {Function} Function that deregister the listener
       */
      (a) => {
        var s;
        return (s = n.current) == null || s.addEventListener(a.name, a.handler), () => {
          var R;
          (R = n.current) == null || R.removeEventListener(a.name, a.handler);
        };
      }
    );
    return () => {
      r.forEach((a) => a());
    };
  }, [l, f, p, g, y, S, h, D, L, A]), {
    View: q("div", {
      style: F,
      ref: c,
      ...E
    }),
    play: T,
    stop: _,
    pause: I,
    setSpeed: P,
    goToAndStop: x,
    goToAndPlay: b,
    setDirection: J,
    playSegments: M,
    setSubframe: N,
    getDuration: V,
    destroy: j,
    animationContainerRef: c,
    animationLoaded: v,
    animationItem: n.current
  };
};
export {
  U as useLottie
};
