import { List }  from "../dataType/List";
/**
 *  @memberof data
 * @class data.ActiveList
 * @since ui.data
 */
export class ActiveList {
  _activeList:object[]=[];
    /**
     * @hideconstructor
     * @return ui.data.ActiveList
     */
    constructor(list?: object[]) {
      if (!(this instanceof ActiveList)) {
        return new ActiveList(list);
      }
      if (list) {
        this._activeList=list;
      }
    }
    /**
     * 添加地图组件到联动列表中。
     * @return  ui.data.ActiveList
     */
    add(el: object) {
      this._activeList.push(el);
      return this;
    }
    /**
     * 遍历回调函数
     * @param {Function} callback 回调
     *
     */
    forEach(callback: Function) {
     for (let index = 0; index < this._activeList.length; index++) {
      const element = this._activeList[index];
       callback(element,index);
     }
    }
    /**
     * 返回指定索引处的元素。
     * @param {Number} index 索引值
     * @return  object
     */
    get(index: number) {
      return this._activeList[index];
    }
    /**
     * 以JS数组的形式返回列表。
     * @returns  List<Object>
     */
    getJsArray() {
      //Returns the list as a JS array.
    }
    /**
     * 在指定的索引位置插入一个元素,并移动列表的其余部分。如果指定的索引大于列表的长度,则元素将被追加到列表中。
     * @param {Number} index 索引值
     * @param {Object} el 元素对象
     * @returns ui.data.ActiveList。
     */
    insert(index: number, el: object) {
      if (index>=this.length()) {
        this._activeList.push(el);
      }
      else{
        this._activeList.splice(index, 0, el);
      }
      return this;
    }
    /**
     * 返回列表中元素的个数。
     * @returns Number
     */
    length() {
      return   this._activeList.length;
    }
    /**
     * 从列表中删除指定元素
     * @param {Object} el 元素
     * @returns 返回删除的元素,如果元素不在列表中则返回null。
     */
    remove(el: object) {
      if(this._activeList.includes(el))
      {
        this._activeList = this._activeList.filter(item => item != el);
        return el;
      }
      else{
        return null;
      }
    }
    /**
     * 将list中的所有元素替换为一个新的list,如果没有提供list,则删除list中的所有元素。
     * @param {List<Object>} list 可选 元素列表。
     * @returns 返回应用重置后列表中的元素。
     */
    reset(list?: List<Object>) {
      //Replaces all elements in list with a new list or, if no list is provided, removes all elements from list.
      //Returns the elements in the list after the reset is applied.
    }
  
    /**
     * 在指定的索引处设置一个元素。如果索引超过列表最后一个元素的索引,则该元素将被添加到列表的末尾。
     * @param {Number} index 索引值
     * @param {Object} el 元素对象
     * @returns  ui.data.ActiveList。
     */
    set(index: number, el: object) {
      if (index>=this.length()) {
        this._activeList.push(el);
      }
      else{
        this._activeList.splice(index, 0, el);
      }
      return this;
    }
  }