import { r as registerInstance, d as createEvent, B as Build, h } from './index-000d2977.js';
import { r as renderHandlebars } from './renderHandlebars-c354e9ac.js';
import './index-4d5a6774.js';
import './_commonjsHelpers-4ed75ef8.js';
import './getHandlebarsHelpers-91de4454.js';
import './logic-88140d62.js';
import './index-d4e79b88.js';

const totalGraphCss = "floodteam-total-graph fireenjin-graph{display:block;margin:0 auto;width:75%}@media only screen and (max-width: 1050px ){floodteam-total-graph fireenjin-graph{width:95%}}";

const TotalGraph = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.sumKeyToIndex = {};
    this.groupToDateFormat = {
      week: "yyyy-Q-MM-ww",
    };
    this.type = "line";
    this.labels = [];
    this.dataSets = [];
    this.groupBy = "month";
    this.options = [];
    this.paths = {};
    this.totals = {};
    this.sumKeys = [];
  }
  ongroupByChanged() {
    this.populateGraph(this.totaller);
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    if (event.detail.endpoint === "findTotaller") {
      this.totaller = ((_b = (_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.totaller) || null;
      this.populateGraph(this.totaller);
    }
    else if (((_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.endpoint) === "listTotals") {
      console.log(event.detail);
      this.filterTotals(((_e = (_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.totals) || []);
      const totallerId = (_j = (_h = (_g = (_f = event === null || event === void 0 ? void 0 : event.detail) === null || _f === void 0 ? void 0 : _f.event) === null || _g === void 0 ? void 0 : _g.detail) === null || _h === void 0 ? void 0 : _h.params) === null || _j === void 0 ? void 0 : _j.totallerId;
      this.totals = {
        ...this.totals,
        [totallerId]: (_l = (_k = event === null || event === void 0 ? void 0 : event.detail) === null || _k === void 0 ? void 0 : _k.data) === null || _l === void 0 ? void 0 : _l.totals,
      };
    }
  }
  filterTotals(allTotals) {
    // formatDate(new Date());
    const labels = [];
    let i = 0;
    for (const totals of (allTotals || []).reverse()) {
      if (this.groupBy === "week" && this.groupToDateFormat["week"] && i > 12)
        continue;
      i = i + 1;
      labels.push(totals === null || totals === void 0 ? void 0 : totals.id);
      this.sumKeys.map((sumKey) => {
        this.dataSets[this.sumKeyToIndex[sumKey]].data.push(totals[sumKey] || 0);
      });
      this.labels = labels;
    }
  }
  populateGraph(totaller) {
    var _a, _b;
    this.sumKeys = Object.keys(((_a = this.totaller) === null || _a === void 0 ? void 0 : _a.sums) || {});
    this.dataSets = Object.values(((_b = this.totaller) === null || _b === void 0 ? void 0 : _b.sums) || {}).map((sum, index) => {
      this.sumKeyToIndex[sum === null || sum === void 0 ? void 0 : sum.id] = index;
      return {
        label: (sum === null || sum === void 0 ? void 0 : sum.name) || (sum === null || sum === void 0 ? void 0 : sum.id),
        data: [],
        borderColor: (sum === null || sum === void 0 ? void 0 : sum.color) || this.randomColor(),
        backgroundColor: (sum === null || sum === void 0 ? void 0 : sum.color) || this.randomColor(),
      };
    });
    const totallerId = totaller === null || totaller === void 0 ? void 0 : totaller.id;
    for (const [key, path] of Object.entries((totaller === null || totaller === void 0 ? void 0 : totaller.paths) || {})) {
      if (!this.paths[totallerId])
        this.paths[totallerId] = {};
      this.paths[totallerId][key] = path;
      if (this.groupBy === key)
        this.fetchTotals(totallerId, path);
      if (!this.options.includes(key))
        this.options.push(key);
    }
    this.options = [...this.options];
    this.paths = { ...this.paths };
  }
  randomNumber() {
    return Math.random() * 10;
  }
  randomColor() {
    return "#" + Math.floor(Math.random() * 16777215).toString(16);
  }
  renderPathTemplate(totallerId, pathStr, timestamp) {
    return renderHandlebars(pathStr, {
      totaller: {
        id: totallerId,
      },
      data: {
        // THIS NEEDS TO BE CHANGED
        createdAt: timestamp || new Date(),
      },
    });
  }
  async fetchTotaller() {
    this.fireenjinFetch.emit({
      endpoint: "findTotaller",
      params: {
        id: this.totallerId,
      },
    });
  }
  async fetchTotals(totallerId, totalsPath, timestamp) {
    let path = this.renderPathTemplate(totallerId, totalsPath, timestamp);
    const pathParts = path.split("/");
    pathParts.pop();
    path = pathParts.join("/");
    this.fireenjinFetch.emit({
      endpoint: "listTotals",
      params: {
        totallerId,
        path,
      },
    });
  }
  async componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.fetchTotaller();
  }
  render() {
    return (h("fireenjin-graph", { type: this.type, labels: this.labels, datasets: this.dataSets, options: {
        plugins: {
          legend: {
            display: true,
            labels: {
              color: "rgb(255, 99, 132)",
            },
          },
        },
      } }));
  }
  static get watchers() { return {
    "groupBy": ["ongroupByChanged"]
  }; }
};
TotalGraph.style = totalGraphCss;

const TotallerCardList = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.groupBy = "month";
    this.timeframe = new Date();
    this.options = [];
    this.paths = {};
    this.totals = {};
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "findTotaller") {
      const totallerId = (_d = (_c = (_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.totaller) === null || _d === void 0 ? void 0 : _d.id;
      for (const [key, path] of Object.entries(((_g = (_f = (_e = event === null || event === void 0 ? void 0 : event.detail) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.totaller) === null || _g === void 0 ? void 0 : _g.paths) || {})) {
        if (!this.paths[totallerId])
          this.paths[totallerId] = {};
        this.paths[totallerId][key] = path;
        if (this.groupBy === key)
          this.fetchTotals(totallerId, path);
        if (!this.options.includes(key))
          this.options.push(key);
      }
      this.options = [...this.options];
      this.paths = { ...this.paths };
      if (!this.label)
        this.label = ((_k = (_j = (_h = event === null || event === void 0 ? void 0 : event.detail) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.totaller) === null || _k === void 0 ? void 0 : _k.name) || null;
    }
    else if (((_l = event === null || event === void 0 ? void 0 : event.detail) === null || _l === void 0 ? void 0 : _l.endpoint) === "findTotals") {
      const totallerId = (_q = (_p = (_o = (_m = event === null || event === void 0 ? void 0 : event.detail) === null || _m === void 0 ? void 0 : _m.event) === null || _o === void 0 ? void 0 : _o.detail) === null || _p === void 0 ? void 0 : _p.params) === null || _q === void 0 ? void 0 : _q.totallerId;
      this.totals = {
        ...this.totals,
        [totallerId]: (_s = (_r = event === null || event === void 0 ? void 0 : event.detail) === null || _r === void 0 ? void 0 : _r.data) === null || _s === void 0 ? void 0 : _s.totals,
      };
    }
  }
  async fetchTotals(totallerId, path, timestamp) {
    this.fireenjinFetch.emit({
      endpoint: "findTotals",
      params: {
        totallerId,
        path: this.renderPathTemplate(totallerId, path, timestamp),
      },
    });
  }
  async onChange(event) {
    var _a, _b, _d;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "groupBy") {
      this.groupBy = (_b = event === null || event === void 0 ? void 0 : event.target) === null || _b === void 0 ? void 0 : _b.value;
    }
    for (const totallerId of this.totallerIds) {
      const noonTimestamp = new Date(Date.parse(event.target.value));
      noonTimestamp.setHours(24);
      this.fetchTotals(totallerId, this.paths[totallerId][this.groupBy], ((_d = event === null || event === void 0 ? void 0 : event.target) === null || _d === void 0 ? void 0 : _d.name) === "timeframe" ? noonTimestamp : this.timeframe);
    }
  }
  renderPathTemplate(totallerId, pathStr, timestamp) {
    return renderHandlebars(pathStr, {
      totaller: {
        id: totallerId,
      },
      data: {
        // THIS NEEDS TO BE CHANGED
        createdAt: timestamp || new Date(),
      },
    });
  }
  render() {
    return [
      h("floodteam-title-bar", null, this.label || "", h("div", { slot: "end" }, h("fireenjin-select", { style: { display: "none" }, ref: (el) => (this.selectEl = el), label: "View By", labelPosition: "stacked", name: "groupBy", value: this.groupBy, options: (this.options || []).map((option) => ({
          label: option,
          value: option,
        })) }), h("fireenjin-chip-bar", null, h("ion-chip", { onClick: () => this.selectEl.querySelector("ion-select").click() }, this.groupBy, h("ion-icon", { name: "chevron-down-circle" })), h("ion-chip", null, h("fireenjin-input", { "clear-on-edit": true, name: "timeframe", type: "date", value: this.timeframe }))))),
      (this.totallerIds || []).map((totallerId) => {
        var _a;
        return (h("floodteam-totaller-cards", { totals: (_a = this.totals) === null || _a === void 0 ? void 0 : _a[totallerId], totallerId: totallerId }));
      }),
    ];
  }
};

export { TotalGraph as floodteam_total_graph, TotallerCardList as floodteam_totaller_card_list };
