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(String(value).includes("<a href")) {
        result = 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)) {
      let result =
        String(value).includes("https:") || String(value).includes("http:")
          ? value
          : null;

      if(String(value).includes("<a href")) {
        result = null;
      }

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

    }
  });
  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") {
      if (value === null || value === "null") {
        return null;
      }
      const unixTimestamp = value;
      const milliseconds = unixTimestamp // * 1000 in some cases.  
      const dateObject = new Date(milliseconds);
      value = dateObject.toLocaleString();
  }
  if (formatType === "UNIXDATE_NOTIME") {
      if (value === null || value === "null") {
        return null;
      }
      const unixTimestamp = value;
      const milliseconds = unixTimestamp // * 1000 in some cases.  
      const dateObject = new Date(milliseconds);
      const dateFormatted = new Intl.DateTimeFormat("en-US", {
          year: "numeric",
          month: "2-digit",
          day: "2-digit"
        }).format(dateObject);
      value = dateFormatted;
  }
  if (formatType === "ICEJAM") {
      let url = `https://icejam.sec.usace.army.mil/ords/f?p=1001:2:::::IR_INDEX_NUMBER:${value}`;
      let result = `<span><a href="${url}" target="_blank"> ${value} </a></span>`;
      return result;
  }

  return value;
};

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