import { PropertyType } from "./types";

export class Unit {
  public id: string;
  public title: string;
  public children: (Property | Unit)[];
  public parent: Unit | null;
  public type: PropertyType = PropertyType.Unit;
  // public data: object = declaration

  constructor(
    title: string,
    data?: object,
    parent?: Unit,
    unitType?: PropertyType
  ) {
    this.id = `${Date.now()}${Math.random()}`;
    this.title = title;
    this.children = [];
    this.parent = parent || null;
    this.type = unitType || PropertyType.Unit;
    // this.data = data || {}

    Object.defineProperty(this, "parent", {
      enumerable: false,
    });
  }

  /**
   * 添加一个输入框子字段
   */
  public addInputChildren(label: string) {
    const p = new Property(label, "", this);
    this.children.push(p);
  }

  /**
   * 将子属性改为单元
   */
  public changeChild2Unit(child: Property): Unit {
    const index = this.children.indexOf(child);
    const newUnit = new Unit(child.label, this);
    this.children.splice(index, 1, newUnit);
    return newUnit;
  }
}

export class declaration {
  public value: string;
  public format: string;
  public type: string;
  constructor(value: string, format: string, type: string) {
    this.value = value;
    this.format = format;
    this.type = type;
  }
}
/**
 * 属性
 */
export class Property {
  public id: string;
  public label: string;
  public key: string;
  public type: PropertyType;
  public options: string[]; // todo 非下拉选项时冗余
  public value: string | Array<string>;
  public parent: Unit;
  public data?: {
    value: string;
    format: string;
    type: string;
  };
  constructor(
    label: string,
    key: string,
    parent: Unit,
    type: PropertyType = PropertyType.Input,
    value: string | Array<string> = ``,
    data?: {
      value: string;
      format: string;
      type: string;
    }
  ) {
    this.id = `${Date.now()}${Math.random()}`;
    this.label = label;
    this.key = key;
    this.type = type;
    this.options = [];
    this.parent = parent;
    this.value = ``;
    this.data = data;

    Object.defineProperties(this, {
      data: {
        enumerable: false,
      },
      key: {
        enumerable: false,
      },
      parent: {
        enumerable: false,
      },
    });

    if (type !== PropertyType.Select) {
      Object.defineProperty(this, "options", {
        enumerable: false,
      });
    }

    if (Object.prototype.toString.call(value) === `[object Array]`) {
      if (
        !(
          value.length !== 0 &&
          Object.prototype.toString.call(value[0]) === `[object Object]`
        )
      ) {
        this.value = value;
      }
    } else {
      if (data && data.value) {
        this.value = data.value;
      } else {
        this.value = String(value);
      }
    }
  }
}
export interface ResBase {
  code: number;
  data: any;
  message: string;
}
export interface ResUploadFiles extends ResBase {
  data: FileItem[];
}

export interface FileItem {
  hash: string;
  name: string;
  type: string;
  url: string;
  update_time: number;
  create_time: number;
}
