import {
  /* inject, */
<% if (isGlobal) { -%>
  globalInterceptor,
<% } else { -%>
  injectable,
<% } -%>
  Interceptor,
  InvocationContext,
  InvocationResult,
  Provider,
  ValueOrPromise,
} from '@loopback/core';

/**
 * This class will be bound to the application as an `Interceptor` during
 * `boot`
 */
<% if (isGlobal) { -%>
@globalInterceptor('<%= group %>', {tags: {name: '<%= name %>'}})
<% } else { -%>
@injectable({tags: {key: <%= className %>.BINDING_KEY}})
<% } -%>
export class <%= className %> implements Provider<Interceptor> {
<% if (!isGlobal) { -%>
  static readonly BINDING_KEY = `interceptors.${<%= className %>.name}`;

<% } -%>
  /*
  constructor() {}
  */

  /**
   * This method is used by LoopBack context to produce an interceptor function
   * for the binding.
   *
   * @returns An interceptor function
   */
  value() {
    return this.intercept.bind(this);
  }

  /**
   * The logic to intercept an invocation
   * @param invocationCtx - Invocation context
   * @param next - A function to invoke next interceptor or the target method
   */
  async intercept(
    invocationCtx: InvocationContext,
    next: () => ValueOrPromise<InvocationResult>,
  ) {
    try {
      // Add pre-invocation logic here
      const result = await next();
      // Add post-invocation logic here
      return result;
    } catch (err) {
      // Add error handling logic here
      throw err;
    }
  }
}
