import { DataModel, transformArrayDataModel, transformDataModel } from '@imengyu/js-request-transform';
import { CommonContentApi, GetContentDetailItem, GetContentListItem, GetContentListParams } from '../CommonContent';

export class TravalListItem extends DataModel<TravalListItem> {
  constructor() {
    super(TravalListItem, "路线列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      longitude: { clientSide: 'number', serverSide: 'number' },
      latitude: { clientSide: 'number', serverSide: 'number' },
    }
    const old = this._afterSolveServer;
    this._afterSolveServer = (data) => {
      old?.(data);
    }
  }
  id = 0;
  longitude = 0;
  latitude = 0;
  title = '';
  desc = '';
  image = '';
  thumbnail = '';
}

export class TravalContentApi extends CommonContentApi {

  constructor() {
    super(undefined, 17, "路线");
  }

  async getTravalList(id: number) {
    const res = await this.getContentDetail(id, GetContentDetailItem, 17, {
      'scenic_spots': '1',
    });
    if (res.scenicSpotsList && Array.isArray(res.scenicSpotsList)) {
      res.scenicSpotsList = (res.scenicSpotsList as any[]).map(p => transformDataModel<TravalListItem>(TravalListItem, p));
      res.keywords = (res.scenicSpotsList as any[]).map(p => p.name);
    }
    return res;
  }
  async getTravalRouteList(params: GetContentListParams) {
    params.setSelfValues({
      pid: 8768,
      modelId: 17,
    });
    const res = await this.getContentList(params, 1, 100);
    res.list.forEach((p) => {
      if (p.scenicSpotsList) {
        p.scenicSpotsList = transformArrayDataModel<TravalListItem>(TravalListItem, p.scenicSpotsList as any[], '路线景点列表', true);
      }
    });
    return res;
  }

}

export default new TravalContentApi();
