/**********************************************************
 * File: "interceptors-agent.mustache"
 *
 * Interceptor Target: {{ name }}
 *  - funcName: {{ target.funcName }}
 *  - Parameters: {{{ nativeParamTypes }}}
 **********************************************************/

/**
 * @link https://github.com/frida/frida/issues/1774#issuecomment-878173544
 *  Thanks to the suggestion from @oleavr:
 *  > GumJS currently configures Interceptor
 *  > so it will ignore calls from Frida's internal threads.
 *  > This only applies to listeners though (onEnter/onLeave)
 *  >  – so if you use Interceptor.replace() you will see calls from Frida's internal threads.
 *  > (And should be prepared to handle that.) Cool project btw!
 */

;(() => {
  const sendSidecarPayloadHook = (...args) => {
    log.verbose(
      'SidecarAgent',
      'Interceptor.attach(%s) onEnter()',
      '{{ target.funcName }}',
    )

    {{{ declareJsArgs }}}

    send(__sidecar__payloadHook(
      '{{ name }}',
      {{{ jsArgs }}}
    ), null)

  }
  const nativeCallback = new NativeCallback(
    sendSidecarPayloadHook,
    {{{ nativeRetType }}},
    {{{ nativeParamTypes }}},
  )

  /**
  *  Huan(202107): `target` at here should be a native callback ptr
  *    which is declared in the `initAgentScript` for workaround
  *
  *  Notice that the `target.funcName` callback will be `Interceptor.replace()`-ed
  *    which means it should not contains any logic code, it should just be a stub.
  */
  Interceptor.replace(
    {{ namespace }}{{ target.funcName }},
    nativeCallback,
  )

})()

