const formatArcgisrest = function (attributes) {
  for (const [key, value] of Object.entries(attributes)) {
    const result =
      String(value).includes("https:") || String(value).includes("http:")
        ? value
        : null;

    if (result) {
      attributes[key] = `<a href="${value}" target="_new" > ${value} </a>`;
    }
  }
  return attributes;
};

const formatArcgisVector = function (results) {
  results.map((feat) => {
    for (const [key, value] of Object.entries(feat.properties)) {
      const result =
        String(value).includes("https:") || String(value).includes("http:")
          ? value
          : null;

      if (result) {
        feat.properties[
          key
        ] = `<a href="${value}" target="_new" > ${value} </a>`;
      }
      // Format UNIX Date
      
      // if ((key === "fromdate" || key === "todate" || key === "referencedate") && Number.isInteger(feat.properties[key])) {
      //   const unixTimestamp = feat.properties[key];
      //   const milliseconds = unixTimestamp // * 1000 in some cases.  
      //   const dateObject = new Date(milliseconds);
      //   feat.properties[key] = dateObject.toLocaleString();
      // }

    }
  });
  return results;
};

const identifyColumnsArcgisrest = function (data, excludedColumns) {
  data.results.forEach((result) => {
    if(result.attributes){
        excludedColumns.forEach((column) => {
          delete result.attributes[column];
        });  
    }
    return result.attributes;
  });
  return data;
};

const identifyExcludeColumnsArcgisvector = function (data, excludedColumns) {
  data.forEach((feature) => {
    if(feature.properties){
        excludedColumns.forEach((column) => {
          delete feature.properties[column];
        });
    }
      return feature.properties
  });
  
  return data;
};

const identifyIncludeColumnsArcgisvector = function (data, identifyFields) {
  data.forEach((feature) => {
    console.log(feature, feature.properties)
    if(feature.properties){
      for (const [key, value] of Object.entries(feature.properties)) {
              let reducedProperties = {};
              identifyFields.forEach((property) => {
                if (feature.properties[property]) {
                  reducedProperties[property] =
                  feature.properties[property];
                }
              });
              feature.properties = reducedProperties; 
       }
    }
  })
  
  return data;
};

const identifyRenameColumnsArcgisvector = function (data, identifyRenameColums) {
  
  data.forEach((feature) => {
    if(feature.properties){
      let wkData = {};
      for (const [featureKey, featureValue] of Object.entries(feature.properties)) {
        for (const [key, value] of Object.entries(identifyRenameColums)){
          if (featureKey === key) {
            // feature.properties[value] = feature.properties[featureKey];
            wkData[value] = feature.properties[featureKey];
            delete feature.properties[featureKey];
          }
        }
      }
      feature.properties = Object.assign(wkData, feature.properties)
    }
  })

  return data;
};

const identifyFormatColumnsArcgisvector = function (data, identifyFormatColumns) {
  data.forEach((feature) => {
    if(feature.properties){
      for (const [key, value] of Object.entries(identifyFormatColumns)){
        console.log(key, value);
        if(feature.properties.hasOwnProperty(key)){
          feature.properties[key] = formatColumns(feature.properties[key], value)
        }
      }  
    }
  })

  return data;
};

const formatColumns = function (value, formatType) {
  if (formatType === "PERCENT") {
    value = (value * 100).toFixed(0) + '%'
  }
  if (formatType === "BOOLEAN") {
    value = value === 0 ? "No" : "Yes"
  }
  if (formatType === "UNIXDATE") {
      const unixTimestamp = value;
      const milliseconds = unixTimestamp // * 1000 in some cases.  
      const dateObject = new Date(milliseconds);
      value = dateObject.toLocaleString();
  }
  return value;
};

export { formatArcgisVector, formatArcgisrest, identifyColumnsArcgisrest, identifyExcludeColumnsArcgisvector, identifyIncludeColumnsArcgisvector, identifyRenameColumnsArcgisvector, identifyFormatColumnsArcgisvector };