import { db } from '../db'

import { UIComponent } from '../controller/statistics'

export default class {
  static async createOne(modal: { 
    project_id: string,
    component_name: string,
    component_list: Array<UIComponent> }
  ) {
    let needUpdate = false

    const item = db.get('statistics').find({
      project_id: modal.project_id
    }).value()

    if(!item) {
      needUpdate = true
    } else {
      needUpdate = item.component_name !== modal.component_name
      || JSON.stringify(item.component_list) !== JSON.stringify(modal.component_list)
    }
    
    await db.get('statistics')
      .updateOrAdd(modal, 'project_id')
      .write()

    return {
      needUpdate,
      ...modal
    }
  }
}
