const shared_consts = require('../tools/shared_nodejs');

const tools = require('../tools/general');

const countryCodes = require('country-codes-list');

module.exports = {

  async executeQueryPickup(idapp, params) {

    const table = params.table;
    const strfind = params.search;

    if (strfind === '') {
      return [];
    }

    let myCountryArr = [];

    let out = {};

    if (table === shared_consts.TAB_COUNTRY) {
      // '[{countryCode}] {countryNameLocal}: +{countryCallingCode}'
      out = {
        id: '{countryCode}',
        value: '{countryNameLocal}',
        flag: '{flag}',
      };

    } else if (table === shared_consts.TAB_PHONES) {
      out = {
        id: '{countryCode}',
        value: '{countryNameLocal} +{countryCallingCode}',
        code: '+{countryCallingCode}',
        flag: '{flag}',
      };
    }

    try {
      // const myCountryArr = countryCodes.filter('countryNameLocal', strfind);
      // const myCountryArr = countryCodes.customList.filter()

      // myCountryArr = countryCodes.customList('countryCode', lavueout, { filter: ((countryData) => { return countryData['countryNameLocal'].toLocaleLowerCase().indexOf(strfind) >= 0 })} );
      myCountryArr = countryCodes.customArray(out, {
        filter: ((countryData) => {
          return countryData['countryNameLocal'].toLocaleLowerCase().
              startsWith(strfind) || countryData['countryNameEn'].toLocaleLowerCase().
              startsWith(strfind);
        }),
      });

    } catch (e) {
      console.log('err', e);
    }

    return myCountryArr;
  },
};
