import { jsx as f } from "react/jsx-runtime";
import T from "react";
import { connect as D } from "react-redux";
import { injectIntl as F } from "react-intl";
import { DataContext as I } from "./DataContext.js";
import { getData as C, setData as y } from "../reducers/data.js";
import "semantic-ui-react";
class S extends T.Component {
  constructor() {
    super(), this.state = {
      showLoading: !1
    }, this.checkLoadingTime = this.checkLoadingTime.bind(this), this.loadingTimeout = null, this.fallbackTimeout = null, this.initialFilterTimeout = null;
  }
  componentDidMount() {
    const { app: t, csv: i, store: a, params: e, source: l, group: n, editing: s, waitForFilters: r = !1 } = this.props;
    console.log("Group & Store ", a, this.props.mySelf), t === "csv" ? this.props.onSetData({ app: t, csv: i, store: a, params: e, group: n }) : (this.setState({ showLoading: !1 }), (!r || s) && (console.log("📥 [D3 Map DataProvider] Initial data load triggered", { app: t, editing: s, waitForFilters: r }), this.loadData({ app: t, source: l, store: a, params: e, group: n })), !s && r && (this.fallbackTimeout = setTimeout(() => {
      !this.props.data && !this.props.loading && (console.warn("⚠️ [D3 Map DataProvider] Fallback loading triggered"), this.loadData({ app: t, source: l, store: a, params: e, group: n }));
    }, 2e3)));
  }
  componentWillUnmount() {
    clearTimeout(this.fallbackTimeout), clearTimeout(this.loadingTimeout), clearTimeout(this.initialFilterTimeout);
  }
  componentDidUpdate(t) {
    const {
      app: i,
      filters: a,
      apply: e,
      source: l,
      store: n,
      params: s,
      csv: r,
      group: d,
      autoApply: g,
      lastInitialFilterChange: c,
      lastUserFilterChange: h,
      waitForFilters: p
    } = this.props;
    if (e != null && e !== t.apply) {
      console.log("🔄 [DataProvider] Manual reload (apply button)"), this.loadData({ app: i, source: l, store: n, params: s, group: d });
      return;
    }
    if (g !== !1 && (a !== t.filters || JSON.stringify(s) !== JSON.stringify(t.params) || i !== t.app || JSON.stringify(t.source) !== JSON.stringify(l) || r !== t.csv))
      if (i === "csv")
        this.props.onSetData({ app: i, csv: r, store: n, params: s, group: d });
      else {
        const m = c !== t.lastInitialFilterChange, u = h !== t.lastUserFilterChange;
        m && p ? (console.log("ℹ️ [DataProvider] Initial filters changing, debouncing..."), this.initialFilterTimeout && clearTimeout(this.initialFilterTimeout), this.initialFilterTimeout = setTimeout(() => {
          console.log("✅ [DataProvider] Initial filters settled, loading data"), this.loadData({ app: i, source: l, store: n, params: s, group: d });
        }, 500)) : u && (console.log("ℹ️ [DataProvider] User filters changed, loading data"), this.loadData({ app: i, source: l, store: n, params: s, group: d }));
      }
  }
  loadData(t) {
    console.log("Loading data for map", t), this.setState({ showLoading: !1 }), this.props.onLoadData(t), this.loadingTimeout && clearTimeout(this.loadingTimeout), this.loadingTimeout = setTimeout(this.checkLoadingTime, 200);
  }
  checkLoadingTime() {
    const { loading: t, time: i } = this.props;
    t && i && (Date.now() - i > 1e3 ? this.setState({ showLoading: !0 }) : this.loadingTimeout = setTimeout(this.checkLoadingTime, 200));
  }
  render() {
    const { data: t } = this.props;
    return /* @__PURE__ */ f(I.Provider, { value: t, children: this.props.children });
  }
}
const L = (o, t) => {
  const { store: i, group: a, app: e } = t;
  return {
    lastUserFilterChange: o.getIn(["data", "filters-settings", e, a, "lastUserFilterChange"]),
    lastInitialFilterChange: o.getIn(["data", "filters-settings", e, a, "lastInitialFilterChange"]),
    data: o.getIn(["data", ...i, "data"]),
    filters: o.getIn(["data", "filters", e, a]),
    autoApply: o.getIn(["data", "filters-settings", e, a, "autoApply"]),
    apply: o.getIn(["data", "filters-settings", e, a, "apply"]),
    error: o.getIn(["data", ...i, "error"]),
    loading: o.getIn(["data", ...i, "loading"]),
    time: o.getIn(["data", ...i, "time"])
  };
}, k = {
  onSetData: y,
  onLoadData: C
}, M = D(L, k)(F(S));
export {
  M as default
};
