(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){(function (){
"use strict";

class e {
  constructor() {
    this.handlers = new Map, this.stackDepth = new Map, this.traceState = {}, this.nextId = 1, 
    this.started = Date.now(), this.pendingEvents = [], this.flushTimer = null, this.cachedModuleResolver = null, 
    this.cachedObjcResolver = null, this.cachedSwiftResolver = null, this.flush = () => {
      if (null !== this.flushTimer && (clearTimeout(this.flushTimer), this.flushTimer = null), 
      0 === this.pendingEvents.length) return;
      const e = this.pendingEvents;
      this.pendingEvents = [], send({
        type: "events:add",
        events: e
      });
    };
  }
  init(e, t, s, n) {
    const o = global;
    o.stage = e, o.parameters = t, o.state = this.traceState;
    for (const e of s) try {
      (0, eval)(e.source);
    } catch (t) {
      throw new Error(`Unable to load ${e.filename}: ${t.stack}`);
    }
    this.start(n).catch((e => {
      send({
        type: "agent:error",
        message: e.message
      });
    }));
  }
  dispose() {
    this.flush();
  }
  update(e, t, s) {
    const n = this.handlers.get(e);
    if (void 0 === n) throw new Error("Invalid target ID");
    const o = this.parseHandler(t, s);
    n[0] = o[0], n[1] = o[1];
  }
  async start(e) {
    const t = {
      native: new Map,
      java: []
    }, s = [];
    for (const [n, o, a] of e) switch (o) {
     case "module":
      "include" === n ? this.includeModule(a, t) : this.excludeModule(a, t);
      break;

     case "function":
      "include" === n ? this.includeFunction(a, t) : this.excludeFunction(a, t);
      break;

     case "relative-function":
      "include" === n && this.includeRelativeFunction(a, t);
      break;

     case "imports":
      "include" === n && this.includeImports(a, t);
      break;

     case "objc-method":
      "include" === n ? this.includeObjCMethod(a, t) : this.excludeObjCMethod(a, t);
      break;

     case "swift-func":
      "include" === n ? this.includeSwiftFunc(a, t) : this.excludeSwiftFunc(a, t);
      break;

     case "java-method":
      s.push([ n, a ]);
      break;

     case "debug-symbol":
      "include" === n && this.includeDebugSymbol(a, t);
    }
    let n, o = !0;
    if (s.length > 0) {
      if (!Java.available) throw new Error("Java runtime is not available");
      n = new Promise(((e, n) => {
        Java.perform((() => {
          o = !1;
          for (const [e, n] of s) "include" === e ? this.includeJavaMethod(n, t) : this.excludeJavaMethod(n, t);
          this.traceJavaTargets(t.java).then(e).catch(n);
        }));
      }));
    } else n = Promise.resolve();
    await this.traceNativeTargets(t.native), o || await n, send({
      type: "agent:initialized"
    }), n.then((() => {
      send({
        type: "agent:started",
        count: this.handlers.size
      });
    }));
  }
  async traceNativeTargets(e) {
    const t = new Map, s = new Map, n = new Map;
    for (const [o, [a, r, i]] of e.entries()) {
      let e;
      switch (a) {
       case "c":
        e = t;
        break;

       case "objc":
        e = s;
        break;

       case "swift":
        e = n;
      }
      let c = e.get(r);
      void 0 === c && (c = [], e.set(r, c)), c.push([ i, ptr(o) ]);
    }
    return await Promise.all([ this.traceNativeEntries("c", t), this.traceNativeEntries("objc", s), this.traceNativeEntries("swift", n) ]);
  }
  async traceNativeEntries(e, s) {
    if (0 === s.size) return;
    const n = this.nextId, o = [], a = {
      type: "handlers:get",
      flavor: e,
      baseId: n,
      scopes: o
    };
    for (const [e, t] of s.entries()) o.push({
      name: e,
      members: t.map((e => e[0]))
    }), this.nextId += t.length;
    const {scripts: r} = await t(a);
    let i = 0;
    for (const e of s.values()) for (const [t, s] of e) {
      const e = n + i, o = "string" == typeof t ? t : t[1], a = this.parseHandler(o, r[i]);
      this.handlers.set(e, a);
      try {
        Interceptor.attach(s, this.makeNativeListenerCallbacks(e, a));
      } catch (e) {
        send({
          type: "agent:warning",
          message: `Skipping "${t}": ${e.message}`
        });
      }
      i++;
    }
  }
  async traceJavaTargets(e) {
    const s = this.nextId, n = [], o = {
      type: "handlers:get",
      flavor: "java",
      baseId: s,
      scopes: n
    };
    for (const t of e) for (const [e, {methods: s}] of t.classes.entries()) {
      const t = e.split("."), o = t[t.length - 1], a = Array.from(s.keys()).map((e => [ e, `${o}.${e}` ]));
      n.push({
        name: e,
        members: a
      }), this.nextId += a.length;
    }
    const {scripts: a} = await t(o);
    return new Promise((t => {
      Java.perform((() => {
        let n = 0;
        for (const t of e) {
          const e = Java.ClassFactory.get(t.loader);
          for (const [o, {methods: r}] of t.classes.entries()) {
            const t = e.use(o);
            for (const [e, o] of r.entries()) {
              const r = s + n, i = this.parseHandler(o, a[n]);
              this.handlers.set(r, i);
              const c = t[e];
              for (const e of c.overloads) e.implementation = this.makeJavaMethodWrapper(r, e, i);
              n++;
            }
          }
        }
        t();
      }));
    }));
  }
  makeNativeListenerCallbacks(e, t) {
    const s = this;
    return {
      onEnter(n) {
        s.invokeNativeHandler(e, t[0], this, n, ">");
      },
      onLeave(n) {
        s.invokeNativeHandler(e, t[1], this, n, "<");
      }
    };
  }
  makeJavaMethodWrapper(e, t, s) {
    const n = this;
    return function(...o) {
      return n.handleJavaInvocation(e, t, s, this, o);
    };
  }
  handleJavaInvocation(e, t, s, n, o) {
    this.invokeJavaHandler(e, s[0], n, o, ">");
    const a = t.apply(n, o), r = this.invokeJavaHandler(e, s[1], n, a, "<");
    return void 0 !== r ? r : a;
  }
  invokeNativeHandler(e, t, s, n, o) {
    const a = Date.now() - this.started, r = s.threadId, i = this.updateDepth(r, o);
    t.call(s, ((...t) => {
      this.emit([ e, a, r, i, t.join(" ") ]);
    }), n, this.traceState);
  }
  invokeJavaHandler(e, t, s, n, o) {
    const a = Date.now() - this.started, r = Process.getCurrentThreadId(), i = this.updateDepth(r, o), c = (...t) => {
      this.emit([ e, a, r, i, t.join(" ") ]);
    };
    try {
      return t.call(s, c, n, this.traceState);
    } catch (e) {
      if (void 0 !== e.$h) throw e;
      Script.nextTick((() => {
        throw e;
      }));
    }
  }
  updateDepth(e, t) {
    const s = this.stackDepth;
    let n = s.get(e) ?? 0;
    return ">" === t ? s.set(e, n + 1) : (n--, 0 !== n ? s.set(e, n) : s.delete(e)), 
    n;
  }
  parseHandler(e, t) {
    try {
      const e = (0, eval)("(" + t + ")");
      return [ e.onEnter ?? f, e.onLeave ?? f ];
    } catch (t) {
      return send({
        type: "agent:warning",
        message: `Invalid handler for "${e}": ${t.message}`
      }), [ f, f ];
    }
  }
  includeModule(e, t) {
    const {native: s} = t;
    for (const t of this.getModuleResolver().enumerateMatches(`exports:${e}!*`)) s.set(t.address.toString(), n(t));
  }
  excludeModule(e, t) {
    const {native: s} = t;
    for (const t of this.getModuleResolver().enumerateMatches(`exports:${e}!*`)) s.delete(t.address.toString());
  }
  includeFunction(e, t) {
    const s = i(e), {native: o} = t;
    for (const e of this.getModuleResolver().enumerateMatches(`exports:${s.module}!${s.function}`)) o.set(e.address.toString(), n(e));
  }
  excludeFunction(e, t) {
    const s = i(e), {native: n} = t;
    for (const e of this.getModuleResolver().enumerateMatches(`exports:${s.module}!${s.function}`)) n.delete(e.address.toString());
  }
  includeRelativeFunction(e, t) {
    const s = c(e), n = Module.getBaseAddress(s.module).add(s.offset);
    t.native.set(n.toString(), [ "c", s.module, `sub_${s.offset.toString(16)}` ]);
  }
  includeImports(e, t) {
    let s;
    if (null === e) {
      const e = Process.enumerateModules()[0].path;
      s = this.getModuleResolver().enumerateMatches(`imports:${e}!*`);
    } else s = this.getModuleResolver().enumerateMatches(`imports:${e}!*`);
    const {native: o} = t;
    for (const e of s) o.set(e.address.toString(), n(e));
  }
  includeObjCMethod(e, t) {
    const {native: s} = t;
    for (const t of this.getObjcResolver().enumerateMatches(e)) s.set(t.address.toString(), o(t));
  }
  excludeObjCMethod(e, t) {
    const {native: s} = t;
    for (const t of this.getObjcResolver().enumerateMatches(e)) s.delete(t.address.toString());
  }
  includeSwiftFunc(e, t) {
    const {native: s} = t;
    for (const t of this.getSwiftResolver().enumerateMatches(`functions:${e}`)) s.set(t.address.toString(), a(t));
  }
  excludeSwiftFunc(e, t) {
    const {native: s} = t;
    for (const t of this.getSwiftResolver().enumerateMatches(`functions:${e}`)) s.delete(t.address.toString());
  }
  includeJavaMethod(e, t) {
    const s = t.java, n = Java.enumerateMethods(e);
    for (const e of n) {
      const {loader: t} = e, n = u(s, (e => {
        const {loader: s} = e;
        return null !== s && null !== t ? s.equals(t) : s === t;
      }));
      if (void 0 === n) {
        s.push(l(e));
        continue;
      }
      const {classes: o} = n;
      for (const t of e.classes) {
        const {name: e} = t, s = o.get(e);
        if (void 0 === s) {
          o.set(e, d(t));
          continue;
        }
        const {methods: n} = s;
        for (const e of t.methods) {
          const t = h(e), s = n.get(t);
          void 0 === s ? n.set(t, e) : n.set(t, e.length > s.length ? e : s);
        }
      }
    }
  }
  excludeJavaMethod(e, t) {
    const s = t.java, n = Java.enumerateMethods(e);
    for (const e of n) {
      const {loader: t} = e, n = u(s, (e => {
        const {loader: s} = e;
        return null !== s && null !== t ? s.equals(t) : s === t;
      }));
      if (void 0 === n) continue;
      const {classes: o} = n;
      for (const t of e.classes) {
        const {name: e} = t, s = o.get(e);
        if (void 0 === s) continue;
        const {methods: n} = s;
        for (const e of t.methods) {
          const t = h(e);
          n.delete(t);
        }
      }
    }
  }
  includeDebugSymbol(e, t) {
    const {native: s} = t;
    for (const t of DebugSymbol.findFunctionsMatching(e)) s.set(t.toString(), r(t));
  }
  emit(e) {
    this.pendingEvents.push(e), null === this.flushTimer && (this.flushTimer = setTimeout(this.flush, 50));
  }
  getModuleResolver() {
    let e = this.cachedModuleResolver;
    return null === e && (e = new ApiResolver("module"), this.cachedModuleResolver = e), 
    e;
  }
  getObjcResolver() {
    let e = this.cachedObjcResolver;
    if (null === e) {
      try {
        e = new ApiResolver("objc");
      } catch (e) {
        throw new Error("Objective-C runtime is not available");
      }
      this.cachedObjcResolver = e;
    }
    return e;
  }
  getSwiftResolver() {
    let e = this.cachedSwiftResolver;
    if (null === e) {
      try {
        e = new ApiResolver("swift");
      } catch (e) {
        throw new Error("Swift runtime is not available");
      }
      this.cachedSwiftResolver = e;
    }
    return e;
  }
}

async function t(e) {
  const t = [], {type: n, flavor: o, baseId: a} = e, r = e.scopes.slice().map((({name: e, members: t}) => ({
    name: e,
    members: t.slice()
  })));
  let i = a;
  do {
    const e = [], a = {
      type: n,
      flavor: o,
      baseId: i,
      scopes: e
    };
    let c = 0;
    for (const {name: t, members: s} of r) {
      const n = [];
      e.push({
        name: t,
        members: n
      });
      let o = !1;
      for (const e of s) if (n.push(e), c++, 1e3 === c) {
        o = !0;
        break;
      }
      if (s.splice(0, n.length), o) break;
    }
    for (;0 !== r.length && 0 === r[0].members.length; ) r.splice(0, 1);
    send(a);
    const l = await s(`reply:${i}`);
    t.push(...l.scripts), i += c;
  } while (0 !== r.length);
  return {
    scripts: t
  };
}

function s(e) {
  return new Promise((t => {
    recv(e, (e => {
      t(e);
    }));
  }));
}

function n(e) {
  const [t, s] = e.name.split("!").slice(-2);
  return [ "c", t, s ];
}

function o(e) {
  const {name: t} = e, [s, n] = t.substr(2, t.length - 3).split(" ", 2);
  return [ "objc", s, [ n, t ] ];
}

function a(e) {
  const {name: t} = e, [s, n] = t.split("!", 2);
  return [ "swift", s, n ];
}

function r(e) {
  const t = DebugSymbol.fromAddress(e);
  return [ "c", t.moduleName ?? "", t.name ];
}

function i(e) {
  const t = e.split("!", 2);
  let s, n;
  return 1 === t.length ? (s = "*", n = t[0]) : (s = "" === t[0] ? "*" : t[0], n = "" === t[1] ? "*" : t[1]), 
  {
    module: s,
    function: n
  };
}

function c(e) {
  const t = e.split("!", 2);
  return {
    module: t[0],
    offset: parseInt(t[1], 16)
  };
}

function l(e) {
  return {
    loader: e.loader,
    classes: new Map(e.classes.map((e => [ e.name, d(e) ])))
  };
}

function d(e) {
  return {
    methods: new Map(e.methods.map((e => [ h(e), e ])))
  };
}

function h(e) {
  const t = e.indexOf("(");
  return -1 === t ? e : e.substr(0, t);
}

function u(e, t) {
  for (const s of e) if (t(s)) return s;
}

function f() {}

const v = new e;

rpc.exports = {
  init: v.init.bind(v),
  dispose: v.dispose.bind(v),
  update: v.update.bind(v)
};

}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})

},{}]},{},[1])
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 
