"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;

var _configSchema = require("@osd/config-schema");

var _common = require("../../common");

var _helpers = require("./utils/helpers");

/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
function _default(router) {
  // Get all configs from Notifications
  router.get({
    path: _common.REPORTING_NOTIFICATIONS_DASHBOARDS_API.GET_CONFIGS,
    validate: {
      query: _configSchema.schema.object({
        from_index: _configSchema.schema.number(),
        max_items: _configSchema.schema.number(),
        query: _configSchema.schema.maybe(_configSchema.schema.string()),
        config_type: _configSchema.schema.oneOf([_configSchema.schema.arrayOf(_configSchema.schema.string()), _configSchema.schema.string()]),
        feature_list: _configSchema.schema.maybe(_configSchema.schema.oneOf([_configSchema.schema.arrayOf(_configSchema.schema.string()), _configSchema.schema.string()])),
        is_enabled: _configSchema.schema.maybe(_configSchema.schema.boolean()),
        sort_field: _configSchema.schema.string(),
        sort_order: _configSchema.schema.string(),
        config_id_list: _configSchema.schema.maybe(_configSchema.schema.oneOf([_configSchema.schema.arrayOf(_configSchema.schema.string()), _configSchema.schema.string()]))
      })
    }
  }, async (context, request, response) => {
    const config_type = (0, _helpers.joinRequestParams)(request.query.config_type);
    const feature_list = (0, _helpers.joinRequestParams)(request.query.feature_list);
    const config_id_list = (0, _helpers.joinRequestParams)(request.query.config_id_list);
    const query = request.query.query; // @ts-ignore

    const client = context.reporting_plugin.notificationsClient.asScoped(request);

    try {
      const resp = await client.callAsCurrentUser('notifications.getConfigs', {
        from_index: request.query.from_index,
        max_items: request.query.max_items,
        is_enabled: request.query.is_enabled,
        sort_field: request.query.sort_field,
        sort_order: request.query.sort_order,
        config_type,
        ...(feature_list && {
          feature_list
        }),
        ...(query && {
          query
        }),
        ...(config_id_list && {
          config_id_list
        })
      });
      return response.ok({
        body: resp
      });
    } catch (error) {
      return response.custom({
        statusCode: error.statusCode || 500,
        body: error.message
      });
    }
  }); // get event by id

  router.get({
    path: `${_common.REPORTING_NOTIFICATIONS_DASHBOARDS_API.GET_EVENT}/{eventId}`,
    validate: {
      params: _configSchema.schema.object({
        eventId: _configSchema.schema.string()
      })
    }
  }, async (context, request, response) => {
    // @ts-ignore
    const client = context.reporting_plugin.notificationsClient.asScoped(request);

    try {
      const resp = await client.callAsCurrentUser('notifications.getEventById', {
        eventId: request.params.eventId
      });
      return response.ok({
        body: resp
      });
    } catch (error) {
      return response.custom({
        statusCode: error.statusCode || 500,
        body: error.message
      });
    }
  }); // Send test message

  router.get({
    path: `${_common.REPORTING_NOTIFICATIONS_DASHBOARDS_API.SEND_TEST_MESSAGE}/{configId}`,
    validate: {
      params: _configSchema.schema.object({
        configId: _configSchema.schema.string()
      }),
      query: _configSchema.schema.object({
        feature: _configSchema.schema.string()
      })
    }
  }, async (context, request, response) => {
    // @ts-ignore
    const client = context.reporting_plugin.notificationsClient.asScoped(request);

    try {
      const resp = await client.callAsCurrentUser('notifications.sendTestMessage', {
        configId: request.params.configId,
        feature: request.query.feature
      });
      return response.ok({
        body: resp
      });
    } catch (error) {
      return response.custom({
        statusCode: error.statusCode || 500,
        body: error.message
      });
    }
  });
}

module.exports = exports.default;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 11700 characters]