import { rest } from 'msw';
import { setupServer } from 'msw/node';
import 'whatwg-fetch';
import * as apiResponse from './api-response';

import config from '../../../config/app';
import { MOBILE_PUSH } from '../../../v2Containers/CreativesContainer/constants';
const API_ENDPOINT = config.development.api_endpoint;
const AUTH_ENDPOINT = config.development.auth_endpoint;
const CAMPAIGNS_API_ENDPOINT = config.development.campaigns_api_org_endpoint;

export const server = setupServer(
  rest.options('*', (req, res, ctx) => res(
    ctx.status(200),
    ctx.set('access-control-allow-origin', '*'),
    ctx.set('Access-Control-Allow-Headers', 'x-cap-org'),
    ctx.set('Access-Control-Allow-Headers', 'x-cap-remote-user'),
    ctx.set('Access-Control-Allow-Headers', 'x-cap-api-data-context-org-id'),
    ctx.set('Access-Control-Allow-Headers', 'x-cap-api-auth-org-id'),
  )),
  rest.get(`${AUTH_ENDPOINT}/auth/org/:orgId/users`, (req, res, ctx) => {
    const { orgId } = req.params;
    switch (orgId) {
      case '50146':
        return res(ctx.status(200), ctx.json(apiResponse.authOrgUsersReonData));
    }
  }),

  rest.get(`${AUTH_ENDPOINT}/user`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.authUserReonData))),

  rest.get(`${AUTH_ENDPOINT}/org/users`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.authOrgUsersReonData))),
  rest.get(`${API_ENDPOINT}/templates/Sms`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.smsTemplates))),
  rest.get(`${API_ENDPOINT}/templates/v1/Sms`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.smsTemplates))),
  rest.get(`${API_ENDPOINT}/templates/Rcs`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.rcsTemplates))),
  rest.get(`${API_ENDPOINT}/templates/v1/Rcs`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.rcsTemplates))),
  rest.get(`${API_ENDPOINT}/meta/wecrm`, (req, res, ctx) => {
    const sourceName = req.url.searchParams.get('source_name');
    switch (sourceName) {
      case 'RCS':
        return res(ctx.status(200), ctx.json(apiResponse.rcsAccount));
      case 'WHATSAPP':
        return res(ctx.status(200), ctx.json(apiResponse.whatsappAccount));
      case 'VIBER':
        return res(ctx.status(200), ctx.json(apiResponse.viberAccount));
      case MOBILE_PUSH:
        return res(ctx.status(200), ctx.json(apiResponse.mpushAccount));
      default:
        return res(ctx.status(200), ctx.json(apiResponse.whatsappAccount));
    }
  }),
  rest.get(`${API_ENDPOINT}/assets/image`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.gallery))),
  rest.get(`${API_ENDPOINT}/templates/v1/Line`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.lineTemplates))),
  rest.get(`${API_ENDPOINT}/templates/v1/Viber`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.viberTemplates))),
  rest.get(`${API_ENDPOINT}/templates/v1/Email`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.emailTemplates))),
  rest.get(`${API_ENDPOINT}/meta/TAG`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.tag))),
  rest.get(`${CAMPAIGNS_API_ENDPOINT}/meta/domainProperties`, (req, res, ctx) => res(ctx.status(200), ctx.json(apiResponse.domainProperties))),
);
