import {
  PropertyType,
  RootUnit as RootUnitType,
  TmplSelectProperty,
  TemplateProperty,
  UnitProperty,
} from "./types";
import { Property, Unit } from "./types2";

/**
 * 将本地模板转换为接口json
 * @param local
 */
export function formatApiJson(local: Unit[]): RootUnitType[] {
  return local.map((unit) => {
    return {
      data: unit.children.map((item) => formatL2Local2Api(item)),
      type: 3,
      key:unit.title,
      label: unit.title
    };
  });
}

/**
 * 将本地的二级字段转换为接口二级字段
 * @param localL2
 */
function formatL2Local2Api(localL2: Property | Unit): TemplateProperty | TmplSelectProperty | UnitProperty {
  if (localL2 instanceof Property) {

    return formatProperty2Api(localL2);
  } else {
    console.log(1);
    
    const children = localL2.children as Property[];
    return {
      key: '',
      type: PropertyType.Unit,
      // label: localL2.title,
      value: children.map((l3) => formatProperty2Api(l3))
    };
  }
}


function formatProperty2Api(property: Property): TemplateProperty | TmplSelectProperty | any {

  if (property.type === PropertyType.Select) {
    return {
      key: property.label,
      type: PropertyType.Select,
      label: property.label,
      options: property.options,
      data: {
        type: "text",
        format: "string(string, html, json, base64)",
        value: property.value,
      },
    };
  } else if (property.type === PropertyType.Image) {
    let data = {}
    if (property.value && property.value !== '') {
      let dd = Array.from(property.value);
      data = dd.map((value: any) => ({
        format: "hash",
        type: "image",
        value: value.hash || ''
      }))
    }
    else {
      data = {}
    }
    return {
      key: property.label,
      type: (property.type as PropertyType.Image),
      data: data,
      label: property.label
    };
  } else if (property.type === PropertyType.File) {
    let data = {}
    if (property.value) {
      let dd = Array.from(property.value);
      data = (dd as any).map((value: any) => ({
        format: "hash",
        type: "file",
        value: value
      }))
    }
    else {
      data = {}
    }
    return {
      key: property.label,
      type: (property.type as PropertyType.File),
      data: data,
      label: property.label
    };
  } else if (property.type === PropertyType.Input) {
    // 保留key，保留data
    return {
      data: {
        format: property.data && property.data.format ? property.data.format : 'string',
        type: property.data && property.data.type ? property.data.type : 'text',
        value: property.value
      },
      type: PropertyType.Input,
      // key: property.parent.title === 'ext' ? property.label : property.key,
      key:property.label,
      label: property.label
    };
  } else if (property.type === PropertyType.Date) {
    return {
      data: {
        format: "utc",
        type: "date",
        value: property.value
      },
      type: PropertyType.Date,
      key: property.label,
      label: property.label
    };

  } else if (property.type === PropertyType.Video) {
    let data = {}
    if (property.value) {
      let dd = Array.from(property.value);
      data = (dd as any).map((value: any) => ({
        format: "hash",
        type: "media",
        value: value
      }))
    }
    else {
      data = {}
    }
    return {
      key: property.label,
      type: (property.type as PropertyType.Video),
      data: data,
      label: property.label
    };
  }
  else if (property.type === PropertyType.Audio) {
    let data = {}
    if (property.value) {
      let dd = Array.from(property.value);
      data = (dd as any).map((value: any) => ({
        format: "hash",
        type: "media",
        value: value
      }))
    }
    else {
      data = {}
    }
    return {
      key: property.label,
      type: (property.type as PropertyType.Audio),
      data: data,
      label: property.label
    };
  }
  //Video 
  else {
    return {
      data: {
        format: "string",
        type: "text",
        value: property.value
      },
      type: (property.type as PropertyType.Image),
      key: property.label,
      label: property.label
    };
  }
}

// 接口json数据转换为本地
export function formatTemplateApi2Local(apiList: RootUnitType[]): Unit[] {
  return apiList.map((rootUnit) => {
    const unit = new Unit(rootUnit.label);
    if (rootUnit.data) {
      unit.children = rootUnit.data.map((l2) => {
        return formatApiProperty2Local(l2, unit);
      })
    }
    return unit;
  })
}
// 接口json数据转换为本地
function formatApiProperty2Local(property: any, parent: Unit): Property {
  const local = new Property(property.label, property.key, parent, property.type, property.value, property.data);
  if (property.type === PropertyType.Select) {
    local.options = (property.options || [])
  } else if (property.type === PropertyType.Image) {
    local.value = property.data
  } else if (property.type === PropertyType.File) {
    local.value = property.data
  } else if (property.type === PropertyType.Video) {
    local.value = property.data
  } else if (property.type === PropertyType.Audio) {
    local.value = property.data
  }
  return local;
}
