import queryString from 'query-string';
import { SHOPEE_HEADER_KEYS } from '@/constant/shopee';

/**
 * @param {BridgeListenerRequestPayload} payload
 * @returns {BridgeListenerParsedRequest}
 */
export const parseRequest = (payload) => {
  const { requestHeaders, url, method } = payload;
  const headers = {};

  const { query } = queryString.parseUrl(url);

  requestHeaders.forEach(({ name, value }) => {
    headers[name] = value;
  });

  return {
    url,
    headers,
    query,
    method,
    isIgnore: !!headers[SHOPEE_HEADER_KEYS.IGNORE],
  };
};
