var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var _a;
import { a as r, j as jsxRuntimeExports, E as compilerRuntimeExports, r as reactExports } from "./vendor-react-DYNCg6N5.js";
import { an as propTypesExports, B as Button } from "./vendor-mantine-h0WlM2Mj.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
const isSafari = () => /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
const isJsons = (array) => Array.isArray(array) && array.every(
  (row) => typeof row === "object" && !(row instanceof Array)
);
const isArrays = (array) => Array.isArray(array) && array.every(
  (row) => Array.isArray(row)
);
const jsonsHeaders = (array) => Array.from(
  array.map((json) => Object.keys(json)).reduce((a, b) => /* @__PURE__ */ new Set([...a, ...b]), [])
);
const jsons2arrays = (jsons, headers) => {
  headers = headers || jsonsHeaders(jsons);
  let headerLabels = headers;
  let headerKeys = headers;
  if (isJsons(headers)) {
    headerLabels = headers.map((header) => header.label);
    headerKeys = headers.map((header) => header.key);
  }
  const data = jsons.map((object) => headerKeys.map((header) => getHeaderValue(header, object)));
  return [headerLabels, ...data];
};
const getHeaderValue = (property, obj) => {
  const foundValue = property.replace(/\[([^\]]+)]/g, ".$1").split(".").reduce(function(o, p, i, arr) {
    const value = o[p];
    if (value === void 0 || value === null) {
      arr.splice(1);
    } else {
      return value;
    }
  }, obj);
  return foundValue === void 0 ? property in obj ? obj[property] : "" : foundValue;
};
const elementOrEmpty = (element) => typeof element === "undefined" || element === null ? "" : element;
const joiner = (data, separator = ",", enclosingCharacter = '"') => {
  return data.filter((e) => e).map(
    (row) => row.map((element) => elementOrEmpty(element)).map((column) => `${enclosingCharacter}${column}${enclosingCharacter}`).join(separator)
  ).join(`
`);
};
const arrays2csv = (data, headers, separator, enclosingCharacter) => joiner(headers ? [headers, ...data] : data, separator, enclosingCharacter);
const jsons2csv = (data, headers, separator, enclosingCharacter) => joiner(jsons2arrays(data, headers), separator, enclosingCharacter);
const string2csv = (data, headers, separator, enclosingCharacter) => headers ? `${headers.join(separator)}
${data}` : data.replace(/"/g, '""');
const toCSV = (data, headers, separator, enclosingCharacter) => {
  if (isJsons(data)) return jsons2csv(data, headers, separator, enclosingCharacter);
  if (isArrays(data)) return arrays2csv(data, headers, separator, enclosingCharacter);
  if (typeof data === "string") return string2csv(data, headers, separator);
  throw new TypeError(`Data should be a "String", "Array of arrays" OR "Array of objects" `);
};
const buildURI = (data, uFEFF, headers, separator, enclosingCharacter) => {
  const csv = toCSV(data, headers, separator, enclosingCharacter);
  const type = isSafari() ? "application/csv" : "text/csv";
  const blob = new Blob([uFEFF ? "\uFEFF" : "", csv], { type });
  const dataURI = `data:${type};charset=utf-8,${uFEFF ? "\uFEFF" : ""}${csv}`;
  const URL = window.URL || window.webkitURL;
  return typeof URL.createObjectURL === "undefined" ? dataURI : URL.createObjectURL(blob);
};
const propTypes = {
  data: propTypesExports.oneOfType([propTypesExports.string, propTypesExports.array, propTypesExports.func]).isRequired,
  headers: propTypesExports.array,
  target: propTypesExports.string,
  separator: propTypesExports.string,
  filename: propTypesExports.string,
  uFEFF: propTypesExports.bool,
  onClick: propTypesExports.func,
  asyncOnClick: propTypesExports.bool,
  enclosingCharacter: propTypesExports.string
};
const defaultProps$1 = {
  separator: ",",
  filename: "generatedBy_react-csv.csv",
  uFEFF: true,
  asyncOnClick: false,
  enclosingCharacter: '"'
};
const defaultProps = {
  target: "_blank"
};
class CSVDownload extends r.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  buildURI() {
    return buildURI(...arguments);
  }
  componentDidMount() {
    const { data, headers, separator, enclosingCharacter, uFEFF, target, specs, replace } = this.props;
    this.state.page = window.open(
      this.buildURI(data, uFEFF, headers, separator, enclosingCharacter),
      target,
      specs,
      replace
    );
  }
  getWindow() {
    return this.state.page;
  }
  render() {
    return null;
  }
}
__publicField(CSVDownload, "defaultProps", Object.assign(
  defaultProps$1,
  defaultProps
));
__publicField(CSVDownload, "propTypes", propTypes);
let CSVLink$1 = (_a = class extends r.Component {
  constructor(props) {
    super(props);
    this.buildURI = this.buildURI.bind(this);
  }
  buildURI() {
    return buildURI(...arguments);
  }
  /**
   * In IE11 this method will trigger the file download
   */
  handleLegacy(event, isAsync = false) {
    if (window.navigator.msSaveOrOpenBlob) {
      event.preventDefault();
      const {
        data,
        headers,
        separator,
        filename,
        enclosingCharacter,
        uFEFF
      } = this.props;
      const csvData = isAsync && typeof data === "function" ? data() : data;
      let blob = new Blob([uFEFF ? "\uFEFF" : "", toCSV(csvData, headers, separator, enclosingCharacter)]);
      window.navigator.msSaveBlob(blob, filename);
      return false;
    }
  }
  handleAsyncClick(event) {
    const done = (proceed) => {
      if (proceed === false) {
        event.preventDefault();
        return;
      }
      this.handleLegacy(event, true);
    };
    this.props.onClick(event, done);
  }
  handleSyncClick(event) {
    const stopEvent = this.props.onClick(event) === false;
    if (stopEvent) {
      event.preventDefault();
      return;
    }
    this.handleLegacy(event);
  }
  handleClick() {
    return (event) => {
      if (typeof this.props.onClick === "function") {
        return this.props.asyncOnClick ? this.handleAsyncClick(event) : this.handleSyncClick(event);
      }
      this.handleLegacy(event);
    };
  }
  render() {
    const {
      data,
      headers,
      separator,
      filename,
      uFEFF,
      children,
      onClick,
      asyncOnClick,
      enclosingCharacter,
      ...rest
    } = this.props;
    const isNodeEnvironment = typeof window === "undefined";
    const href = isNodeEnvironment ? "" : this.buildURI(data, uFEFF, headers, separator, enclosingCharacter);
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      "a",
      {
        download: filename,
        ...rest,
        ref: (link) => this.link = link,
        target: "_self",
        href,
        onClick: this.handleClick(),
        children
      }
    );
  }
}, __publicField(_a, "defaultProps", defaultProps$1), __publicField(_a, "propTypes", propTypes), _a);
const CSVLink = CSVLink$1;
const CSVLinkComponent = (t0) => {
  const $ = compilerRuntimeExports.c(24);
  const {
    text,
    filename,
    columns,
    csvData,
    actionsFilters,
    getCsvData,
    isLoading
  } = t0;
  const {
    t
  } = useTranslation();
  const [download, setDownload] = reactExports.useState(false);
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = r.createRef();
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const csvLink = t1;
  let t2;
  if ($[1] !== columns) {
    t2 = columns.map(_temp);
    $[1] = columns;
    $[2] = t2;
  } else {
    t2 = $[2];
  }
  const csvHeaders = t2;
  let t3;
  if ($[3] !== actionsFilters || $[4] !== getCsvData) {
    t3 = async () => {
      await getCsvData(actionsFilters);
      setDownload(true);
    };
    $[3] = actionsFilters;
    $[4] = getCsvData;
    $[5] = t3;
  } else {
    t3 = $[5];
  }
  const getData = t3;
  let t4;
  let t5;
  if ($[6] !== csvData || $[7] !== download) {
    t4 = () => {
      var _a2;
      if (download && csvData) {
        setDownload(false);
        (_a2 = csvLink.current) == null ? void 0 : _a2.link.click();
      }
    };
    t5 = [csvData, download];
    $[6] = csvData;
    $[7] = download;
    $[8] = t4;
    $[9] = t5;
  } else {
    t4 = $[8];
    t5 = $[9];
  }
  reactExports.useEffect(t4, t5);
  let t6;
  if ($[10] !== isLoading || $[11] !== t || $[12] !== text) {
    t6 = isLoading ? t("statistics.load_csv") : text;
    $[10] = isLoading;
    $[11] = t;
    $[12] = text;
    $[13] = t6;
  } else {
    t6 = $[13];
  }
  let t7;
  if ($[14] !== getData || $[15] !== t6) {
    t7 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { "data-cy": "exportStatistics", size: "sm", radius: 8, onClick: getData, children: t6 });
    $[14] = getData;
    $[15] = t6;
    $[16] = t7;
  } else {
    t7 = $[16];
  }
  const t8 = csvData ?? "";
  const t9 = filename + ".csv";
  let t10;
  if ($[17] !== csvHeaders || $[18] !== t8 || $[19] !== t9) {
    t10 = /* @__PURE__ */ jsxRuntimeExports.jsx(CSVLink, { data: t8, filename: t9, className: "hidden", ref: csvLink, target: "_blank", headers: csvHeaders });
    $[17] = csvHeaders;
    $[18] = t8;
    $[19] = t9;
    $[20] = t10;
  } else {
    t10 = $[20];
  }
  let t11;
  if ($[21] !== t10 || $[22] !== t7) {
    t11 = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      t7,
      t10
    ] });
    $[21] = t10;
    $[22] = t7;
    $[23] = t11;
  } else {
    t11 = $[23];
  }
  return t11;
};
function _temp(column) {
  return {
    label: column.title,
    key: column.dataIndex
  };
}
export {
  CSVLinkComponent as C
};
//# sourceMappingURL=index-C8AOGyjn.js.map
