import { Build, Component, Event, h, Prop, Listen, State, Watch, forceUpdate, } from "@stencil/core";
import renderHandlebars from "../../../helpers/renderHandlebars";
export class TotalGraph {
  constructor() {
    this.sumKeyToIndex = {};
    this.type = "bar";
    this.labels = [];
    this.dataSets = [];
    this.groupBy = "month";
    this.options = [];
    this.paths = {};
    this.totals = {};
    this.sumKeys = [];
  }
  ongroupByChanged() {
    forceUpdate(this.fetchTotaller());
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
    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.sumKeys = Object.keys(((_c = this.totaller) === null || _c === void 0 ? void 0 : _c.sums) || {});
      this.dataSets = Object.values(((_d = this.totaller) === null || _d === void 0 ? void 0 : _d.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 = (_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.id;
      for (const [key, path] of Object.entries(((_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.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 (((_l = event === null || event === void 0 ? void 0 : event.detail) === null || _l === void 0 ? void 0 : _l.endpoint) === "listTotals") {
      console.log(event.detail);
      const labels = [];
      for (const totals of ((_o = (_m = event === null || event === void 0 ? void 0 : event.detail) === null || _m === void 0 ? void 0 : _m.data) === null || _o === void 0 ? void 0 : _o.totals) || []) {
        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;
      const totallerId = (_s = (_r = (_q = (_p = event === null || event === void 0 ? void 0 : event.detail) === null || _p === void 0 ? void 0 : _p.event) === null || _q === void 0 ? void 0 : _q.detail) === null || _r === void 0 ? void 0 : _r.params) === null || _s === void 0 ? void 0 : _s.totallerId;
      this.totals = {
        ...this.totals,
        [totallerId]: (_u = (_t = event === null || event === void 0 ? void 0 : event.detail) === null || _t === void 0 ? void 0 : _t.data) === null || _u === void 0 ? void 0 : _u.totals,
      };
    }
  }
  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 }));
  }
  static get is() { return "floodteam-total-graph"; }
  static get originalStyleUrls() { return {
    "$": ["total-graph.css"]
  }; }
  static get styleUrls() { return {
    "$": ["total-graph.css"]
  }; }
  static get properties() { return {
    "totallerId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "totaller-id",
      "reflect": false
    },
    "type": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"bar\" | \"line\" | \"pie\"",
        "resolved": "\"bar\" | \"line\" | \"pie\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "type",
      "reflect": false,
      "defaultValue": "\"bar\""
    },
    "totaller": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "Totaller",
        "resolved": "{ __typename?: \"Totaller\"; collection?: string; createdAt?: string; createdBy?: User; id?: string; lastRunAt?: string; name?: string; paths?: any; sampleData?: any; sums?: any; updatedAt?: string; updatedBy?: User; }",
        "references": {
          "Totaller": {
            "location": "import",
            "path": "../../../sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "labels": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "string[]",
        "resolved": "string[]",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "[]"
    },
    "dataSets": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "any[]",
        "resolved": "any[]",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "[]"
    },
    "groupBy": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "group-by",
      "reflect": false,
      "defaultValue": "\"month\""
    }
  }; }
  static get states() { return {
    "options": {},
    "paths": {},
    "totals": {},
    "sumKeys": {}
  }; }
  static get events() { return [{
      "method": "fireenjinFetch",
      "name": "fireenjinFetch",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinFetchEvent",
        "resolved": "FireEnjinFetchEvent",
        "references": {
          "FireEnjinFetchEvent": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }]; }
  static get watchers() { return [{
      "propName": "groupBy",
      "methodName": "ongroupByChanged"
    }]; }
  static get listeners() { return [{
      "name": "fireenjinSuccess",
      "method": "onSuccess",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=total-graph.js.map