'use strict';

const getComponentQuery = (field, options) =>{

    const orQuery = [
        {"documentId": field.componentName},
        {"name" : field.componentName }
    ];

    if (field.documentId) {
        orQuery.push({"documentId": field.documentId});
    }

    let query = {$and:
          [
              {"systemHeader.systemType" : SharedConstants.SYSTEM_TYPE_COMPONENT},
              { $or: orQuery }
          ]
    };

    return query;
};

const getComponentLibrariesQuery = (libDocs, options) =>{

    let docIds = [];
    let serviceNames = [];

    for (let i=0; i<libDocs.length; i++){
        const item = libDocs[i];
        if (item.documentId){
            docIds.push(item.documentId);
        }
        else{
            serviceNames.push(item.name);
        }
    }

    let statement = {$and:
          [{"systemHeader.systemType" : SharedConstants.SYSTEM_TYPE_COMPONENT},
              {$or: [{"documentId" :{$in: docIds}},
                      {"serviceName" :{$in:serviceNames}}
                  ]
              }
          ]
    };

    return statement;

};

const getListComponentResourcesQuery = (options) =>{
    return { "systemHeader.systemType" : SharedConstants.SYSTEM_TYPE_COMPONENT};
};

const getVendorLibraryQuery = (name, options) =>{

    let statement = {$and:
          [{"systemHeader.systemType" : SharedConstants.SYSTEM_TYPE_VENDOR_LIBRARY},
              {
                  $or: [
                    {"documentId" : name},
                    {"name" : name}
                ]
            }
          ]
    };

    return statement;

};

const getVendorLibrariesQuery = (options) =>{
    return {"systemHeader.systemType" : SharedConstants.SYSTEM_TYPE_VENDOR_LIBRARY };
};

module.exports = {

    getComponentQuery: getComponentQuery,

    getComponentLibrariesQuery: getComponentLibrariesQuery,

    getListComponentResourcesQuery:  getListComponentResourcesQuery,

    getVendorLibraryQuery,

    getVendorLibrariesQuery

};