/**
 * Meta information that can be passed to the analytics payload callback
 *
 * @param currentDoc - Current document node after the transaction
 */

/**
 * Callback accepts EditorState
 *
 * @remarks
 * The plugin using the Analytics may using an Optional<Analytics> dependency
 * It can be undefined during testing, we may not have included analytics
 * Also in cases where Analytics fails to load, the editor doesn't crash.
 *
 * @param state - EditorState, passed to the callback
 * @param meta - Meta, passed to the callback
 */

/**
 * Analytics API
 * @example
 * ```
 * withAnalytics({
 * eventType: EVENT_TYPE.TRACK,
 * action: ACTION.REPLACED_ONE,
 *  actionSubject: ACTION_SUBJECT.TEXT,
 *   attributes: {
 *    triggerMethod,
 *  },
 * })(replace(replaceText))
 * ```
 *
 * @param analyticsApi - EditorAnalyticsAPI, undefined in tests or if analytics fails to load
 * @param payload - AnalyticsEventPayload | AnalyticsEventPayloadCallback - payload to be attached to the transaction
 * @param channel - string - channel to be used for analytics
 */
export function withAnalytics(analyticsApi, payload, channel) {
  return function (command) {
    return function (state, dispatch, view) {
      return command(state, function (tr) {
        if (dispatch) {
          if (payload instanceof Function) {
            var dynamicPayload = payload(state, {
              currentDoc: tr.doc
            });
            if (dynamicPayload) {
              analyticsApi === null || analyticsApi === void 0 || analyticsApi.attachAnalyticsEvent(dynamicPayload, channel)(tr);
              dispatch(tr);
            }
          } else {
            analyticsApi === null || analyticsApi === void 0 || analyticsApi.attachAnalyticsEvent(payload, channel)(tr);
            dispatch(tr);
          }
        }
      }, view);
    };
  };
}