/**
 * The message handler IDs.
 * @type {string}
 */
// LINT.IfChange
const SEND_FEEDBACK_NAME = 'sendFeedback';
const PROMOTED_PRODUCT_LINK_NAME = 'promotedProductLink';
const HELP_ACTION_NAME = 'helpAction';
const SEARCHBOX_NAME = 'searchBox';
const SUPPORT_URL_NAME = 'supportURL';
const REJOIN_CHAT_NAME = 'rejoinChat';
const LOG_NAME = 'log';
const NONUNIFIED_LOG_NAME = 'nonUnifiedLog';
const ON_ERROR = 'onError';
// LINT.ThenChange(../GHKMainWebViewController.m:message_handler_names)

/**
 * The message keys.
 * @type {string}
 */
// LINT.IfChange
const PROMOTED_PRODUCT_LINK_RESULT_ID = 'promotedProductLinkResultID';
const HELP_ACTION_RESULT_JSON = 'helpActionResultJSON';
const SUPPORT_URL = 'supportURL';
const METRIC_DATA_STRING = 'metricDataString';
const METRICS_DATA_STRING = 'metricsDataString';
const CASE_ID = 'caseID';
const CHAT_SUPPORT_REQUEST_ID = 'chatSupportRequestID';
const JS_ERROR_MESSAGE = 'jsErrorMessage';
const JS_ERROR_URL = 'jsErrorURL';
// LINT.ThenChange(../GHKMainWebViewController.m:message_keys)

/**
 * Defines the callback function for the Help Panel object, which posts a
 * message to be read by HelpKit's main WebView.
 * @param {!Page} page The page that contains the Help Panel object.
 */
window.in_app = function(page) {
  const helpPanelObj = page.getChild();
  if (helpPanelObj == undefined) {
    return;
  }
  helpPanelObj.setSendFeedbackCallback(function(result) {
    window.webkit.messageHandlers[SEND_FEEDBACK_NAME].postMessage({});
  });
  helpPanelObj.setPromotedProductLinkCallback(function(result) {
    window.webkit.messageHandlers[PROMOTED_PRODUCT_LINK_NAME].postMessage({
      [PROMOTED_PRODUCT_LINK_RESULT_ID]: result.promotedProductLinkResultId,
    });
  });
  helpPanelObj.setHelpActionCallback(function(helpAction) {
    window.webkit.messageHandlers[HELP_ACTION_NAME].postMessage({
      [HELP_ACTION_RESULT_JSON]: helpAction.result,
    });
  });
  helpPanelObj.setSearchBoxCallback(function(result) {
    window.webkit.messageHandlers[SEARCHBOX_NAME].postMessage({});
  });
  helpPanelObj.setSupportUrlCallback(function(result) {
    window.webkit.messageHandlers[SUPPORT_URL_NAME].postMessage({
      [SUPPORT_URL]: result.url,
    });
  });
  helpPanelObj.setRejoinChatCallback(function(result) {
    window.webkit.messageHandlers[REJOIN_CHAT_NAME].postMessage({
      [CASE_ID]: result.caseId,
      [CHAT_SUPPORT_REQUEST_ID]: result.chatSupportRequestId,
    });
  });
  helpPanelObj.setLogCallback(function(result) {
    window.webkit.messageHandlers[LOG_NAME].postMessage({
      [METRIC_DATA_STRING]: result.base64EncodedMetricData,
    });
  });
  helpPanelObj.setNonUnifiedLogCallback(function(result) {
    window.webkit.messageHandlers[NONUNIFIED_LOG_NAME].postMessage({
      [METRICS_DATA_STRING]: result.base64EncodedMetricsData,
    });
  });
};

/**
 * Defines the js error callback function, which is invoked when an error occurs
 * during js loading.
 * @param {string} errorMsg The js error message.
 * @param {string} url The URL that js error occurs when loading.
 * @param {!integer} lineNumber The line number that js error occurs.
 */
window.onerror = function(errorMsg, url, lineNumber) {
  window.webkit.messageHandlers[ON_ERROR].postMessage({
    [JS_ERROR_MESSAGE]: errorMsg,
    [JS_ERROR_URL]: url,
  });
};
