import { jsx as d, jsxs as L } from "react/jsx-runtime";
import w from "react";
import { connect as I } from "react-redux";
import { injectIntl as k } from "react-intl";
import { DataContext as F } from "./DataContext.js";
import { getData as O, setData as v } from "../reducers/data.js";
import { Container as m, Segment as p, Dimmer as C, Loader as N } from "semantic-ui-react";
import A from "../../_virtual/debounce.js";
class J extends w.Component {
  constructor() {
    super(), this.state = {
      showLoading: !1
    }, this.checkLoadingTime = this.checkLoadingTime.bind(this), this.debounces = [];
  }
  debouncedLoadData(t, i) {
    const a = A((e) => {
      console.log(`🔄 [DataProvider] Debounced load triggered (${t}ms delay)`, {
        args: e,
        timestamp: (/* @__PURE__ */ new Date()).toISOString()
      }), this.setState({ showLoading: !1 }), this.props.onLoadData(e), this.dataLoaded = !0, this.checkLoadingTime = this.checkLoadingTime.bind(this), setTimeout(this.checkLoadingTime, 0);
    }, t);
    this.debounces.push(a(i));
  }
  componentDidMount() {
    const { app: t, source: i, store: a, params: e, csv: h, group: s, editing: o, waitForFilters: n = !1 } = this.props;
    t === "csv" ? this.props.onSetData({ app: t, csv: h, store: a, params: e, group: s }) : (this.setState({ showLoading: !1 }), !n || o ? (console.log("📥 [DataProvider] Initial data load triggered", {
      app: t,
      source: i,
      store: a,
      params: e,
      group: s,
      waitForFilters: n,
      editing: o,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    }), this.props.onLoadData({ app: t, source: i, store: a, params: e, group: s }), setTimeout(this.checkLoadingTime, 100)) : console.log("⏳ [DataProvider] Waiting for filters before loading data", {
      app: t,
      source: i,
      store: a,
      params: e,
      group: s,
      waitForFilters: n,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    })), !o && n && (this.fallbackTimeout = setTimeout(() => {
      this.dataLoaded || (console.warn("⚠️ [DataProvider] Fallback loading triggered - filters took too long", {
        app: t,
        source: i,
        store: a,
        params: e,
        group: s,
        dataLoaded: this.dataLoaded,
        timestamp: (/* @__PURE__ */ new Date()).toISOString()
      }), this.setState({ showLoading: !1 }), this.props.onLoadData({ app: t, source: i, store: a, params: e, group: s }), setTimeout(this.checkLoadingTime, 100));
    }, 1e3));
  }
  componentWillUnmount() {
    clearTimeout(this.fallbackTimeout), clearTimeout(this.debounceTimeout), this.debounces.forEach((t) => t ? t.cancel() : null);
  }
  componentDidUpdate(t, i, a) {
    const {
      app: e,
      filters: h,
      apply: s,
      source: o,
      store: n,
      params: r,
      csv: g,
      group: l,
      autoApply: T,
      editing: u,
      lastInitialFilterChange: b,
      lastUserFilterChange: y
    } = this.props, f = b !== t.lastInitialFilterChange, S = y !== t.lastUserFilterChange;
    let D = !1;
    s != null && s != t.apply && (D = !0), T !== !1 ? (h != t.filters || JSON.stringify(r) != JSON.stringify(t.params) || e != t.app || JSON.stringify(t.source) != JSON.stringify(o) || g != t.csv) && (e === "csv" ? this.props.onSetData({ app: e, csv: g, store: n, params: r, group: l }) : (this.setState({ showLoading: !0 }), u && this.props.onLoadData({ app: e, source: o, store: n, params: r, group: l }), f && this.props.waitForFilters ? (clearTimeout(this.debounceTimeout), clearTimeout(this.fallbackTimeout), this.debounceTimeout = setTimeout(() => {
      this.dataLoaded = !0, this.setState({ showLoading: !1 }), console.log("🔄 [DataProvider] Loading data after initial filter setup", {
        app: e,
        source: o,
        store: n,
        params: r,
        group: l,
        filters: h,
        debounceDelay: "100ms",
        timestamp: (/* @__PURE__ */ new Date()).toISOString()
      }), this.props.onLoadData({ app: e, source: o, store: n, params: r, group: l }), setTimeout(this.checkLoadingTime, 100);
    }, 100)) : S ? (console.log("🔧 [DataProvider] User filter change detected", {
      currentFilters: h,
      previousFilters: t.filters,
      currentParams: r,
      previousParams: t.params,
      app: e,
      source: o,
      store: n,
      group: l,
      debounceDelay: "400ms",
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    }), this.setState({ showLoading: !1 }), this.debouncedLoadData(400, { app: e, source: o, store: n, params: r, group: l }), setTimeout(this.checkLoadingTime, 100)) : !u && (JSON.stringify(r) !== JSON.stringify(t.params) || e !== t.app || JSON.stringify(o) !== JSON.stringify(t.source)) ? (this.setState({ showLoading: !1 }), this.props.onLoadData({ app: e, source: o, store: n, params: r, group: l }), setTimeout(this.checkLoadingTime, 100)) : console.log("ℹ️ [DataProvider] Component updated but no filter changes detected", {
      initialChanged: f,
      userChanged: S,
      waitForFilters: this.props.waitForFilters,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    }))) : D && (console.log("🔄 [DataProvider] Manual reload triggered (apply button)", {
      app: e,
      source: o,
      store: n,
      params: r,
      group: l,
      apply: s,
      previousApply: t.apply,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    }), this.props.onLoadData({ app: e, source: o, store: n, params: r, group: l }), this.setState({ showLoading: !1 }), setTimeout(this.checkLoadingTime, 100));
  }
  checkLoadingTime() {
    const { data: t, loading: i, time: a, error: e, verbose: h = !0 } = this.props, s = Date.now() - a;
    i && a && s > 1e3 ? this.setState({ showLoading: !0 }) : i && setTimeout(this.checkLoadingTime, 100);
  }
  render() {
    const { data: t, style: i, loading: a, time: e, error: h, editing: s, isSvg: o, verbose: n = !0 } = this.props;
    if (a && this.state.showLoading && !s) {
      const r = {
        width: "100%",
        height: "100%",
        background: "transparent",
        verticalAlign: "middle",
        overflow: "hidden"
      }, g = Object.assign({}, i, {
        height: "90%",
        background: "transparent",
        textAlign: "center",
        margin: "30px"
      }), l = /* @__PURE__ */ d(p, { basic: !0, padded: !0, style: g, children: /* @__PURE__ */ d(C, { active: !0, inverted: !0, style: { background: "transparent" }, children: /* @__PURE__ */ d(N, { size: "medium", style: { background: "transparent" } }) }) });
      return o ? /* @__PURE__ */ d("foreignObject", { style: r, children: /* @__PURE__ */ d(m, { style: i, className: "loading", children: l }) }) : /* @__PURE__ */ d(m, { style: i, className: "loading", children: l });
    } else return h ? h ? /* @__PURE__ */ L(p, { color: "red", children: [
      /* @__PURE__ */ d("h1", { children: "500" }),
      /* @__PURE__ */ d("p", { children: "Wasn't able to load data" })
    ] }) : /* @__PURE__ */ d(m, { children: /* @__PURE__ */ L(p, { color: "red", children: [
      /* @__PURE__ */ d("h1", { children: "404" }),
      /* @__PURE__ */ d("p", { children: "Can't find this page" })
    ] }) }) : /* @__PURE__ */ d(F.Provider, { value: t, children: this.props.children });
  }
}
const x = (c, t) => {
  const { store: i, group: a, app: e } = t;
  return {
    lastUserFilterChange: c.getIn(["data", "filters-settings", e, a, "lastUserFilterChange"]),
    lastInitialFilterChange: c.getIn(["data", "filters-settings", e, a, "lastInitialFilterChange"]),
    data: c.getIn(["data", ...i, "data"]),
    filters: c.getIn(["data", "filters", e, a]),
    autoApply: c.getIn(["data", "filters-settings", e, a, "autoApply"]),
    apply: c.getIn(["data", "filters-settings", e, a, "apply"]),
    error: c.getIn(["data", ...i, "error"]),
    loading: c.getIn(["data", ...i, "loading"]),
    time: c.getIn(["data", ...i, "time"])
  };
}, U = {
  onSetData: v,
  onLoadData: O
}, q = I(x, U)(k(J));
export {
  q as default
};
