import { __assign as M, __spreadArray as $, __read as _ } from "../../../../tslib/tslib.es6.es.js";
import y, { cancelSync as U } from "../../../../framesync/dist/es/index.es.js";
import { motionValue as k } from "../value/index.es.js";
import { isMotionValue as D } from "../value/utils/is-motion-value.es.js";
import { variantPriorityOrder as ee } from "./utils/animation-state.es.js";
import { createLifecycles as ne } from "./utils/lifecycles.es.js";
import { updateMotionValuesFromProps as te } from "./utils/motion-values.es.js";
import { checkIfControllingVariants as re, checkIfVariantNode as ie, isVariantLabel as oe } from "./utils/variants.es.js";
var Ve = function(u) {
  var R = u.treeType, P = R === void 0 ? "" : R, G = u.build, S = u.getBaseTarget, H = u.makeTargetAnimatable, J = u.measureViewportBox, K = u.render, Q = u.readValueFromInstance, W = u.removeValueFromRenderState, B = u.sortNodePosition, N = u.scrapeMotionValuesFromProps;
  return function(d, c) {
    var t = d.parent, i = d.props, X = d.presenceId, Y = d.blockInitialAnimation, F = d.visualState, Z = d.shouldReduceMotion;
    c === void 0 && (c = {});
    var h = !1, a = F.latestValues, v = F.renderState, l, m = ne(), s = /* @__PURE__ */ new Map(), V = /* @__PURE__ */ new Map(), x = {}, A = M({}, a), p;
    function T() {
      !l || !h || (L(), K(l, v, i.style, r.projection));
    }
    function L() {
      G(r, v, a, c, i);
    }
    function E() {
      m.notifyUpdate(a);
    }
    function j(e, n) {
      var o = n.onChange(function(g) {
        a[e] = g, i.onUpdate && y.update(E, !1, !0);
      }), f = n.onRenderRequest(r.scheduleRender);
      V.set(e, function() {
        o(), f();
      });
    }
    var w = N(i);
    for (var b in w) {
      var O = w[b];
      a[b] !== void 0 && D(O) && O.set(a[b], !1);
    }
    var q = re(i), C = ie(i), r = M(M({
      treeType: P,
      current: null,
      depth: t ? t.depth + 1 : 0,
      parent: t,
      children: /* @__PURE__ */ new Set(),
      presenceId: X,
      shouldReduceMotion: Z,
      variantChildren: C ? /* @__PURE__ */ new Set() : void 0,
      isVisible: void 0,
      manuallyAnimateOnMount: Boolean(t == null ? void 0 : t.isMounted()),
      blockInitialAnimation: Y,
      isMounted: function() {
        return Boolean(l);
      },
      mount: function(e) {
        h = !0, l = r.current = e, r.projection && r.projection.mount(e), C && t && !q && (p = t == null ? void 0 : t.addVariantChild(r)), s.forEach(function(n, o) {
          return j(o, n);
        }), t == null || t.children.add(r), r.setProps(i);
      },
      unmount: function() {
        var e;
        (e = r.projection) === null || e === void 0 || e.unmount(), U.update(E), U.render(T), V.forEach(function(n) {
          return n();
        }), p == null || p(), t == null || t.children.delete(r), m.clearAllListeners(), l = void 0, h = !1;
      },
      addVariantChild: function(e) {
        var n, o = r.getClosestVariantNode();
        if (o)
          return (n = o.variantChildren) === null || n === void 0 || n.add(e), function() {
            return o.variantChildren.delete(e);
          };
      },
      sortNodePosition: function(e) {
        return !B || P !== e.treeType ? 0 : B(r.getInstance(), e.getInstance());
      },
      getClosestVariantNode: function() {
        return C ? r : t == null ? void 0 : t.getClosestVariantNode();
      },
      getLayoutId: function() {
        return i.layoutId;
      },
      getInstance: function() {
        return l;
      },
      getStaticValue: function(e) {
        return a[e];
      },
      setStaticValue: function(e, n) {
        return a[e] = n;
      },
      getLatestValues: function() {
        return a;
      },
      setVisibility: function(e) {
        r.isVisible !== e && (r.isVisible = e, r.scheduleRender());
      },
      makeTargetAnimatable: function(e, n) {
        return n === void 0 && (n = !0), H(r, e, i, n);
      },
      measureViewportBox: function() {
        return J(l, i);
      },
      addValue: function(e, n) {
        r.hasValue(e) && r.removeValue(e), s.set(e, n), a[e] = n.get(), j(e, n);
      },
      removeValue: function(e) {
        var n;
        s.delete(e), (n = V.get(e)) === null || n === void 0 || n(), V.delete(e), delete a[e], W(e, v);
      },
      hasValue: function(e) {
        return s.has(e);
      },
      getValue: function(e, n) {
        var o = s.get(e);
        return o === void 0 && n !== void 0 && (o = k(n), r.addValue(e, o)), o;
      },
      forEachValue: function(e) {
        return s.forEach(e);
      },
      readValue: function(e) {
        var n;
        return (n = a[e]) !== null && n !== void 0 ? n : Q(l, e, c);
      },
      setBaseTarget: function(e, n) {
        A[e] = n;
      },
      getBaseTarget: function(e) {
        if (S) {
          var n = S(i, e);
          if (n !== void 0 && !D(n))
            return n;
        }
        return A[e];
      }
    }, m), {
      build: function() {
        return L(), v;
      },
      scheduleRender: function() {
        y.render(T, !1, !0);
      },
      syncRender: T,
      setProps: function(e) {
        (e.transformTemplate || i.transformTemplate) && r.scheduleRender(), i = e, m.updatePropListeners(e), x = te(r, N(i), x);
      },
      getProps: function() {
        return i;
      },
      getVariant: function(e) {
        var n;
        return (n = i.variants) === null || n === void 0 ? void 0 : n[e];
      },
      getDefaultTransition: function() {
        return i.transition;
      },
      getTransformPagePoint: function() {
        return i.transformPagePoint;
      },
      getVariantContext: function(e) {
        if (e === void 0 && (e = !1), e)
          return t == null ? void 0 : t.getVariantContext();
        if (!q) {
          var n = (t == null ? void 0 : t.getVariantContext()) || {};
          return i.initial !== void 0 && (n.initial = i.initial), n;
        }
        for (var o = {}, f = 0; f < ae; f++) {
          var g = z[f], I = i[g];
          (oe(I) || I === !1) && (o[g] = I);
        }
        return o;
      }
    });
    return r;
  };
}, z = $(["initial"], _(ee), !1), ae = z.length;
export {
  Ve as visualElement
};
//# sourceMappingURL=index.es.js.map
