<script>
import { ref, watch, computed } from 'vue';
import { SessionUser } from "@/components/session/token/SessionUser.vue";

// Define the reactive properties
// Data for the table
const dataColumns = ref([]);
const dataRows = ref([]);

// Setters and getters for dataColumns
export const setDataColumns = (columns) => {
  // Default functions
  let defaultFunctions = {
    getLabel: function () {
      if (this.properties.label) {
        return this.properties.label;
      }
      return this.id;
    },
    getType: function () {
      return this.type;
    },
    getProperties: function () {
      return this.properties;
    },
  };
  // Push the default functions to each column
  columns.forEach((column) => {
    // Add the default functions to the column
    for (const key in defaultFunctions) {
      if (Object.prototype.hasOwnProperty.call(defaultFunctions, key)) {
        column[key] = defaultFunctions[key].bind(column);
      }
    }
  });
  // Set the dataColumns
  dataColumns.value = columns;
};
export const getDataColumns = () => {
  return dataColumns.value;
};

// Setters and getters for dataRows
export const setDataRows = (rows) => {
  // Default functions
  let defaultFunctions = {
    parseValue: function (value) {
      // This function is here, to make it possible to override the data parsing elsewhere.
      return value;
    },
    getValue: function (key) {
      return this.parseValue(this[key]);
    },
  };
  // Push the default functions to each row
  rows.forEach((row) => {
    // Add the default functions to the row
    for (const key in defaultFunctions) {
      if (Object.prototype.hasOwnProperty.call(defaultFunctions, key)) {
        row[key] = defaultFunctions[key].bind(row);
      }
    }
  });
  // Set the dataRows
  dataRows.value = rows;
};
export const getDataRows = () => {
  return dataRows.value;
};

// Metadata for the table
const title = ref("Mass Data Inserter");
const subtitle = ref("Indsæt data i systemet");

// Setters and getters for metadata
export const setTitle = (newTitle) => {
  title.value = newTitle;
};
export const getTitle = () => {
  return title.value;
};
export const setSubtitle = (newSubtitle) => {
  subtitle.value = newSubtitle;
};
export const getSubtitle = () => {
  return subtitle.value;
};

// Data structure parser
export const parsedDataFormatFunction = ref((data) => {
  // This function is here, to make it possible to override the data parsing elsewhere.
  return data;
});
export const setParsedDataFormatFunction = (newFunction) => {
  parsedDataFormatFunction.value = newFunction;
};
export const getParsedDataFormatFunction = () => {
  return parsedDataFormatFunction.value;
};

// Save function
export const saveFunction = ref(() => {
  // This function is here, to make it possible to override the data saving elsewhere.
  console.log("Save function not set");
});
export const setSaveFunction = (newFunction) => {
  saveFunction.value = newFunction;
};
export const getSaveFunction = () => {
  return saveFunction.value;
};

// Progress
const request_limit = ref(5);
const pending = ref([]);
const processing = ref([]);
const completed = ref([]);
const failed = ref([]);
const responses = ref([]);
export const progress = computed(() => {
  return {
    pending: pending.value,
    processing: processing.value,
    completed: completed.value,
    failed: failed.value,
    total: pending.value.length + processing.value.length + completed.value.length + failed.value.length,
  };
});
export const setRequestLimit = (limit) => {
  request_limit.value = limit;
};
export const getRequestLimit = () => {
  return request_limit.value;
};
export const setPending = (newPending) => {
  pending.value = newPending;
};
export const getPending = () => {
  return pending.value;
};
export const setProcessing = (newProcessing) => {
  processing.value = newProcessing;
};
export const getProcessing = () => {
  return processing.value;
};
export const setCompleted = (newCompleted) => {
  completed.value = newCompleted;
};
export const getCompleted = () => {
  return completed.value;
};
export const setFailed = (newFailed) => {
  failed.value = newFailed;
};
export const getFailed = () => {
  return failed.value;
};
export const setResponses = (newResponses) => {
  responses.value = newResponses;
};
export const getResponses = () => {
  return responses.value;
};
export const getResponse = (object) => {
  // Get the response for the object
  return getResponses().find((response) => response.object === object);
}


// Clear the progress
export const clearProgress = () => {
  pending.value = [];
  processing.value = [];
  completed.value = [];
  failed.value = [];
  responses.value = [];
};




</script>