fcf.module({
  name: "fcfDBControls:templates/form.receive.js",
  dependencies: [],
  module: function(){
    var self = this;
    return {
      receive: function(a_fields, a_files, a_request){
        if (fcf.empty(a_fields.data))
          return;
        if (a_fields.type == "add"){
          let query = [];
          let isFirst = true;
          fcf.each(a_fields.data, (a_lang, a_values)=>{
            if (isFirst) {
              isFirst = false;
              query.push({
                type: "insert",
                from: a_fields.projection,
                values: fcf.array(a_values, (a_key, a_value)=>{ return { field: a_key, value: a_value }}),
                language: a_lang,
              })
            } else {
              query.push({
                type: "update",
                from: a_fields.projection,
                values: fcf.array(a_values, (a_key, a_value)=>{ return { field: a_key, value: a_value }}),
                where:  [{logic: "and", type: "=", args: [ {function: "key"}, {result: 0, record: 0, item: "@key"} ]}],
                language: a_lang
              })
            }
          })
          return fcf.query(query)
          .then((a_data)=>{
          }); 
        } else if (a_fields.type == "edit"){
          let query = [];
          fcf.each(a_fields.data, (a_lang, a_values)=>{
            query.push({
              type: "update",
              from: a_fields.projection,
              values: fcf.array(a_values, (a_key, a_value)=>{ return { field: a_key, value: a_value }}),
              where:  [{logic: "and", type: "=", args: [ {function: "key"}, {value: a_fields.key} ]}],
              language: a_lang,
            })
          })
          return fcf.query(query)
          .then(()=>{
          });
        }
      }
    }
  }
});
