import messager from '../common/messager';
import prepareHUYA from '../common/prepareHUYA';
import utils from '../common/utils'

export default {
  async request({ header, url, method, data, dataType }) {
    const { HUYA } = await prepareHUYA();
    const req = new HUYA.RouteEBSV2Req();
    if (header) {
      Object.keys(header).forEach(k => req.ebsRequest.header.put(k, header[k]));
    }
    req.ebsRequest.url = url;
    req.ebsRequest.httpMethod = method;
    req.ebsRequest.bodyString = JSON.stringify(data);

    const resp = await messager.dispatchTaf('routeEbsRequestV2', req, HUYA.RouteEBSV2Resp, 'extRouteUI');
    const { ebsResponse } = resp || {};
    const { entity, statusCode, header: respHeader } = ebsResponse || {};

    return {
      header: { ...respHeader.value },
      data: utils.unescapeHTML(entity),
      statusCode
    };
  }
} 