var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tabularize_layers_data_exports = {};
__export(tabularize_layers_data_exports, {
  getColumnHeaders: () => getColumnHeaders,
  tabularizeLayersData: () => tabularizeLayersData
});
module.exports = __toCommonJS(tabularize_layers_data_exports);
var import_lodash_es = require("lodash");
var import_core = require("@dynatrace/strato-components-preview/core");
const NESTED_LEVELS_SUPPORTED = 2;
const buildColumnsHeadersMapping = (columns) => new Map([...columns].map((value, index) => [value, index]));
const getObjectHeaders = (data, objectKey = "", nestingLevel = 0) => {
  let columnHeaders = [];
  if (nestingLevel <= NESTED_LEVELS_SUPPORTED) {
    for (let i = 0; i < data.length; i++) {
      for (const [key, value] of Object.entries(data[i])) {
        const objectPath = [objectKey, key].filter((a) => a).join(".");
        if (!(0, import_lodash_es.isObject)(value)) {
          columnHeaders.push(objectPath);
        } else if (!Array.isArray(value)) {
          const headers = getObjectHeaders(
            [value],
            objectPath,
            nestingLevel + 1
          );
          columnHeaders = columnHeaders.concat(headers);
        }
      }
    }
  }
  return columnHeaders;
};
const getColumnHeaders = (data) => {
  const objectColumnHeaders = getObjectHeaders(data);
  const columnHeadersSet = new Set(objectColumnHeaders);
  const sortedHeaders = ["layer-name", "latitude", "longitude"];
  const startingHeaders = sortedHeaders.reduce(
    (headersList, column) => {
      if (columnHeadersSet.has(column)) {
        headersList.push(column);
      }
      return headersList;
    },
    []
  );
  return /* @__PURE__ */ new Set([...startingHeaders, ...objectColumnHeaders]);
};
const tabularizeLayersData = (data) => {
  let csv = "";
  if (data.length > 0) {
    const columnHeaders = getColumnHeaders(data);
    csv += `${[...columnHeaders].join(",")}
`;
    const columnCount = columnHeaders.size;
    const headersMapping = buildColumnsHeadersMapping(columnHeaders);
    for (const datum of data) {
      const row = [];
      row.length = columnCount;
      for (const [column, index] of headersMapping) {
        let value = (0, import_core._accessValue)(datum, column);
        if (!(0, import_lodash_es.isUndefined)(value)) {
          if (typeof value === "string" && value.includes(",")) {
            value = `"${value}"`;
          }
          row[index] = value;
        }
      }
      csv += `${row.join(",")}
`;
    }
  }
  return csv;
};
