
import JSONbig from 'json-bigint'
export default {

  toBigInt(id) {
    return JSONbig.parse(`{"v":${id}}`).v;
  },

  trim(str) {
    return str.replace(/^\s+|\s+$/gm, '');
  },


  formatDetailData({ data }) {

    if (data.create_time) {
      data.create_time = new Date(data.create_time * 1000);
    }
    if (data.update_time) {
      data.update_time = new Date(data.update_time * 1000);
    }


    return data;

  },

  getPostFieldValue({ dataId, list, type, deletedList, fieldName, rowFilter, dataRule }) {

    let d = {};
    console.log(list)
    if (rowFilter) {
      list = list.filter(rowFilter);
    }
    if (dataRule) {
      list.forEach(item => {

        
        dataRule.forEach(rule => {
          let value = item[rule.field];
          if (rule.type == "integer") {
            item[rule.field] = parseInt(value);
          }
          else if (rule.type == "number") {
            item[rule.field] = parseFloat(value);
          }
          else if (rule.type == "timestamp") {
            item[rule.field] = parseInt(new Date(value).getTime() / 1000);
          }
          else if (rule.type == "bigint") {
            if(typeof(item[rule.field]) == 'string'){
              item[rule.field] = JSONbig.parse(`{"v":${item[rule.field]}}`).v;
            }
          
          }
        })

      })

    }
    if (fieldName) {
      list.forEach(item => {
        item[fieldName] = dataId;
      })
    }
    if (dataId && type !== 'array') {



      d = {
        insertList: [],
        updateList: [],
        deleteList: []
      };

      list.forEach(item => {
        if (item.id) {
          d.updateList.push(item);
        }
        else {
          d.insertList.push(item);
        }
      });

      deletedList.forEach(item => {
        d.deleteList.push(item);
      })


    } else {
      d = list;
    }
    return d;
  }
}