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

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

function typeJson2String(json: Property[]) {
  let temp:any = {};
  json.forEach((item) => {
    temp[item.label] = item.value;
  })
  const value = JSON.stringify(temp);

  return {
    type: "text",
    format: "string",
    value
  };

}

/**
 * 将本地的二级字段转换为接口二级字段
 * @param localL2
 */
function formatL2Local2Api(localL2: Property | Unit): TemplateProperty | TmplSelectProperty | UnitProperty {
  if (localL2 instanceof Property) {
    return formatProperty2Api(localL2);
  } else {
    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.key,
      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(hash)",
        type: "image",
        value: value.hash || ''
      }))
    }
    else {
      data.push({
        format: "hash(hash)",
        type: "image",
        value: ''
      })
    }
    return {
      key: property.key,
      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(hash)",
        type: "file",
        value: value
      }))
    }
    else {
      data.push({
        format: "hash(hash)",
        type: "file",
        value: ''
      })
    }
    return {
      key: property.key,
      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,
      label: property.label
    };
  } else if (property.type === PropertyType.JSON ) {
    return {
      data: {
        format: 'string',
        type: 'text',
        value: property.value
      },
      type: property.type,
      key: property.key,
      label: property.label
    };
  } else if (property.type === PropertyType.Date) {
    return {
      data: {
        format: "utc",
        type: "date",
        value: property.value
      },
      type: PropertyType.Date,
      key: property.key,
      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(hash)",
        type: "media",
        value: value
      }))
    }
    else {
      data.push({
        format: "hash(hash)",
        type: "media",
        value: ''
      })
    }
    return {
      key: property.key,
      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(hash)",
        type: "media",
        value: value
      }))
    }
    else {
      data.push({
        format: "hash(hash)",
        type: "media",
        value: ''
      })
    }
    return {
      key: property.key,
      type: (property.type as PropertyType.Audio),
      data: data,
      label: property.label
    };
  }
  else if (property.type === PropertyType.TextArray || property.type === PropertyType.ImageUrl) {
    let data = property.value === '' ? [{
      format: "string",
      type: "text",
      value: ''
    }] : (property.value as [string])?.map((item:string) => {
      return {
        format: "string",
        type: "text",
        value: item
      }
    });
    return {
      key: property.key,
      type: (property.type as (PropertyType.TextArray | PropertyType.ImageUrl)),
      data: data,
      label: property.label
    };
  }
  //Video
  else {
    return {
      data: {
        format: "string",
        type: "text",
        value: property.value
      },
      type: (property.type as PropertyType.Image),
      key: property.key,
      label: property.label
    };
  }
}

// 接口json数据转换为本地
export function formatTemplateApi2Local(apiList: RootUnitType[]): Unit[] {
  return apiList.map((rootUnit) => {
    const unit = new Unit(rootUnit.label, {}, undefined, rootUnit.type);
    if (Array.isArray(rootUnit.data)) {
      unit.children = rootUnit.data.map((l2) => {
        return formatApiProperty2Local(l2, unit);
      })
    } else {
      let list = [];
      let o = JSON.parse((rootUnit.data as {value: string})?.value || '{}');
      for (let i in o) {
        list.push({
          type: 0,
          label: i,
          key: '',
          value: o[i]
        })
      }
      unit.children = list.map(item => {
        return new Property(item.label,
          item.key,
          unit,
          0,
          item.value,
          {
            format: "string",
            type: "text",
            value: item.value
          });
      })
    }
    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
  } else if (property.type === PropertyType.TextArray || property.type === PropertyType.ImageUrl) {
    local.value = property.data.map((item: {value: string}) => item.value);
  }
  return local;
}
