import { isArray, isNil, isPlainObject } from 'lodash';
import { BaseDataManager } from './baseDataManager';

export default class ScrapingDataManger extends BaseDataManager {
  static formatRequestObject(record) {
    const { ...rest } = record || {};
    return {
      ...rest,
    };
  }

  static formatResponseObject(scrapingData) {
    return scrapingData;
  }

  static convertResponseData(responseData) {
    if (!responseData) return;

    if (isPlainObject(responseData)) {
      if (!isNil(responseData?.page) && !isNil(responseData?.pageSize)) {
        // pagination case
        const text = new TextDecoder('utf-8').decode(responseData?.data);
        const jsonObject = JSON.parse(text);
        return {
          ...responseData,
          total: responseData?.total?.low + responseData?.total?.high * Math.pow(2, 32),
          data: jsonObject,
        };
      }

      return this.formatResponseObject(responseData);
    } else if (isArray(responseData)) {
      return responseData.map((item) => this.formatResponseObject(item));
    } else {
      return responseData;
    }
  }
}
