import { Subject } from "rxjs";
import { Http } from "../Http";
import { ExportAsync } from "./ExportAsync";
import { FeatureCollection } from "../models/FeatureCollection";
import { Feature } from "../models/Feature";
import { Geometry } from "../models/Geometry";
import { AstHelper } from "@gvol-org/geovis-brain-core";
/**
 * @class Export.table
 * @since Export
 */
export class table extends ExportAsync {
  // /**
  //  * @hideconstructor
  //  * @returns Export.table
  //  */
  constructor(...param: any[]) {
    super();
    if (!(this instanceof table)) {
      return new table(param);
    }
  }

  /**
   * 导出FeatureCollection矢量文件为Geojson 
 *  table:导出FeatureCollection,只接受FeatureCollection格式的文件;
 *  description:导出任务描述也就是任务名称;
 *  assetId:用户空间的资源Id;
 *  region:导出范围;
 *  scale:分辨率;
 *  path: 导出路径;
 *  fileFormat: 可选参数,默认GeoJSON;
   * @param {Object} args 批处理对象
   * @tutorial Export.table
   */
  static toDrive(args: {
    table: FeatureCollection | Feature | Geometry | any;
    description?: string;
    assetId?: string;
    region?: Geometry;
    scale?: number;
    path?: string;
    fileFormat?: string;
  }) {
    if (args.scale) {
      Http.scale = args.scale.toString();
    }
    let newObj = JSON.parse(JSON.stringify(args));
    let { newChain, newOrder } = AstHelper.objectParamChain(newObj);

    newChain.push({
      "functionName": "Export.table.toDrive",
      "arguments": {
        "args": newObj
      },
      "order": newOrder
    })
    let content = {
      type: 'map',
      chain: newChain
    };
    if ((window as any).astCallback) {
      (window as any).astCallback(content);
      return;
    }
    let exportObj = new ExportAsync();
    exportObj.execute(async () => {
      let data = await Http.getRequest(newChain);
      Http.responseSubject$.next(data);
      if (data && data.data.status == "success") {
        exportObj.param = data;
        // console.log("subjecttttt", this.exportSubject$);
        this.exportSubject$.next(data.data.detail);
      } else {
        // console.log("应当删除export")
      }
    });
    return exportObj;
  }
}