export default {
  getChangedRelation({relationList, tableName, fks, parentValue, itemCode, isForm, relationOpenFks}) {
    if (relationOpenFks === 'closed') {
      return fks
    }
    if (!relationList || !tableName && !itemCode) {
      return fks
    }
    if (itemCode) {
      itemCode = "temp-root-" + itemCode
    }
    const fksMap = {}
    if (fks) {
      fks.forEach(item => {
        fksMap[item.colCode] = item.queryValue
      })
    }
    let changed = false
    const newFks = []
    let record = {}
    let tempMainFormRelations = null;
    if (isForm) {
      tempMainFormRelations = relationList.find(item => item.toTable === tableName && item.relationType === 'temp-main-form')
    }
    if (tempMainFormRelations) {
      newFks.push({
        colCode: tempMainFormRelations.toCol,
        queryValue: tempMainFormRelations.value,
        relationType: tempMainFormRelations.relationType,
      })
      if (fksMap[tempMainFormRelations.toCol] !== tempMainFormRelations.value) {
        changed = true
        record = tempMainFormRelations.record || {}
      }
    } else {
      relationList.forEach(item => {
        if ((item.toTable === tableName || item.toTable === itemCode) && item.relationType !== 'temp-main-form') {
          newFks.push({
            colCode: item.toCol,
            queryValue: item.value,
          })
          if (fksMap[item.toCol] !== item.value) {
            changed = true
            record = item.record || {}
          }
        }
      })
    }

    if (changed) {
      if (parentValue) {
        for (const key in parentValue) {
          delete parentValue[key]
        }
        Object.assign(parentValue, record)
      }
      return newFks
    }
    return fks
  },
  setChangeValue(relationList, {tableName, record, itemCode, isForm, relationStatus}) {
    if (relationStatus === 'closed') {
      return relationList
    }
    if (!tableName && !itemCode) {
      return relationList
    }
    if (itemCode) {
      itemCode = "temp-root-" + itemCode
    }
    let changed = false
    relationList.forEach(item => {
      if (item.fromTable === tableName || item.fromTable === itemCode) {
        const value = record[item.fromCol] || null
        if (value !== item.value) {
          if (!isForm || item.relationType !== 'temp-main-form') {
            item.value = value
            item.record = record
            changed = true
          }
        }
      }
    })
    if (changed) {
      return [...relationList]
    }
    return relationList
  },
  isParentTable({relationList, tableName, value, itemCode, relationStatus}) {
    if (relationStatus === 'closed') {
      return false
    }
    if (!relationList || !tableName && !itemCode || relationList.length === 0) {
      return undefined
    }
    if (typeof value != "undefined") {
      return value
    }
    if (itemCode) {
      itemCode = "temp-root-" + itemCode
    }
    const relation = relationList.find(item => item.fromTable === tableName || item.fromTable === itemCode)
    return !!relation
  },
  checkFks({fks}) {
    const fk = fks.find(item => !item.queryValue)
    return !fk
  }
}
