import { r as registerInstance, d as createEvent, h } from './index-33e43734.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';

let 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;
    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 };
    }
    else if (((_h = event === null || event === void 0 ? void 0 : event.detail) === null || _h === void 0 ? void 0 : _h.endpoint) === "findTotals") {
      const totallerId = (_m = (_l = (_k = (_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.event) === null || _k === void 0 ? void 0 : _k.detail) === null || _l === void 0 ? void 0 : _l.params) === null || _m === void 0 ? void 0 : _m.totallerId;
      this.totals = {
        ...this.totals,
        [totallerId]: (_p = (_o = event === null || event === void 0 ? void 0 : event.detail) === null || _o === void 0 ? void 0 : _o.data) === null || _p === void 0 ? void 0 : _p.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, _c;
    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], ((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.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, 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 }));
      }),
    ];
  }
  static get watchers() { return {
    "groupBy": ["onSuccess"]
  }; }
};

export { TotallerCardList as floodteam_totaller_card_list };

//# sourceMappingURL=floodteam-totaller-card-list.entry.js.map