const apiKey = '8ALsk8jDOlynEwn8ScMBSnG87';

let indexRange = (arr, start, end) => {
  return arr.slice(start, end)
}

function main() {
  var folder = DriveApp.getFolderById("15p4vXVQd6hhcMl73VOxD8VctL2gR-q43")
  var files = folder.getFiles()
  while (files.hasNext()) {
    var file = files.next();
    //var date = "Wed Jan 10 2024 15:01:58 GMT-0500 (Eastern Standard Time)"
    var getDate = new Date();
    var date = Utilities.formatDate(getDate, 'America/New_York', 'MM/dd/yyyy');
    var fileDate = file.getLastUpdated();
    var fileDateFormatted = Utilities.formatDate(fileDate, 'America/New_York', 'MM/dd/yyyy');
    if (date == fileDateFormatted) {
      var updated_file = file.getBlob();
      var updated_blob = updated_file.getDataAsString();
      var updated_data = Utilities.parseCsv(updated_blob, ',');
    }
    }
    quickParse(updated_data);
    //parseCSV(outdated_data, updated_data)
  }

function quickParse(updated_data) {
  for (var x = 1; x < updated_data.length; x++) {
    var array = updated_data[x];
    for (var i = 1; i < array.length; i++) {
      var item = array[i]
      if (item != "") {
        var api_url = 'https://api.northpass.com/v2/groups/?filter[name][eq]='+item;
        const settings = {
        async: true,
        crossDomain: true,
        method: 'GET',
        headers: {
                accept: 'application/json',
                'X-Api-Key': apiKey
          }
        };
        const sendMsg = UrlFetchApp.fetch(api_url, settings);
        var uuidResponse = sendMsg.getContentText();
        var parseData = JSON.parse(uuidResponse)
        var groupID = parseData['data'][0]['id']
        var groupName = array.indexOf(item);
        if (groupName != -1) {
            array[groupName] = groupID;
        }
      }
    }
    const dom = array.shift();
    const groups = array;
    Logger.log(groups)
    const domain_to_update = { domain: dom, group_ids: groups }
    Logger.log(domain_to_update)
    }
  }
