import { isArray, isNil, isPlainObject } from 'lodash';

export class BaseDataManager {
  static convertRequestData(requestData) {
    if (!requestData) return;

    if (isPlainObject(requestData)) {
      return this.formatRequestObject(requestData);
    } else if (isArray(requestData)) {
      return requestData.map((item) => this.formatRequestObject(item));
    } else {
      return requestData;
    }
  }

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

    if (isPlainObject(responseData)) {
      if (!isNil(responseData?.page) && !isNil(responseData?.pageSize)) {
        // pagination case
        return {
          ...responseData,
          data: (responseData?.data || []).map((item) => this.formatResponseObject(item)),
        };
      }

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

  // Placeholder methods to be overridden in subclasses
  static formatRequestObject() {
    throw new Error('formatRequestObject() not implemented');
  }

  static formatResponseObject() {
    throw new Error('formatResponseObject() not implemented');
  }
}
