import { Build, Component, Event, h, Listen, State, Prop, } from "@stencil/core";
import state from "../../../store";
import getStatusOptions from "../../../helpers/getStatusOptions";
import isInternalTeam from "../../../helpers/auth/isInternalTeam";
import isAdmin from "../../../helpers/auth/isAdmin";
export class AppDashboard {
  constructor() {
    this.groupBy = "week";
    this.unreadCount = 0;
    this.selectedStatuses = ["book", "evaluate", "claim", "mitigate", "monitor", "review"];
    this.totals = {};
    this.showMap = false;
  }
  async onTrigger(event) {
    var _a, _b, _c, _d, _e;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name) === "set" &&
      ((_c = (_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.payload) === null || _c === void 0 ? void 0 : _c.name) === "status") {
      await this.mapEl.clearMarkers();
      this.selectedStatuses = ((_e = (_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.payload) === null || _e === void 0 ? void 0 : _e.value) || [];
    }
  }
  async onChange(event) {
    var _a, _b, _c, _d;
    console.log(event);
    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;
    }
    else if (((_c = event === null || event === void 0 ? void 0 : event.target) === null || _c === void 0 ? void 0 : _c.name) === "timeframe") {
      this.timeframe = (_d = event === null || event === void 0 ? void 0 : event.target) === null || _d === void 0 ? void 0 : _d.value;
    }
  }
  filterJobsByStatus(jobs, statuses) {
    return jobs.filter((job) => statuses.includes(job === null || job === void 0 ? void 0 : job.status));
  }
  async componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.options = await this.db.list("/totals/1mFFMb6HglD2asYliHDm/jobs", null);
    this.fireenjinSubmit.emit({
      endpoint: "exportCsv",
      data: {
        collection: "jobs",
      },
    });
  }
  render() {
    var _a, _b;
    const myJobs = this.filterJobsByStatus(Object.values((state === null || state === void 0 ? void 0 : state.jobs) || {}).filter((job) => { var _a, _b, _c; return (_b = (_a = job === null || job === void 0 ? void 0 : job.userIds) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, (_c = state === null || state === void 0 ? void 0 : state.session) === null || _c === void 0 ? void 0 : _c.uid); }), this.selectedStatuses);
    return [
      h("ion-content", { class: "truck-background ion-padding" },
        isInternalTeam() && isAdmin() && (h("div", { id: "performance" },
          h("floodteam-total-graph", { groupBy: this.groupBy, totallerId: "1mFFMb6HglD2asYliHDm" }),
          h("floodteam-totaller-card-list", { groupBy: this.groupBy, totallerIds: ["1mFFMb6HglD2asYliHDm"] }))),
        h("article", { id: "myjobs" },
          this.showMap && (h("fireenjin-map", { googleMapsKey: (_b = (_a = state === null || state === void 0 ? void 0 : state.config) === null || _a === void 0 ? void 0 : _a.firebase) === null || _b === void 0 ? void 0 : _b.apiKey, ref: (el) => (this.mapEl = el), markers: myJobs.map((job) => {
              var _a, _b;
              return ({
                payload: { job },
                name: job === null || job === void 0 ? void 0 : job.title,
                icon: null,
                position: {
                  lat: (_a = job === null || job === void 0 ? void 0 : job.address) === null || _a === void 0 ? void 0 : _a.lat,
                  lng: (_b = job === null || job === void 0 ? void 0 : job.address) === null || _b === void 0 ? void 0 : _b.lng,
                },
              });
            }) })),
          h("floodteam-title-bar", null,
            h("ion-router-link", { href: "/jobs" },
              "My Jobs",
              " ",
              h("ion-badge", { style: { borderRadius: "15px" } },
                h("small", null, myJobs.length || 0))),
            h("ion-buttons", { slot: "end" },
              h("ion-button", { onClick: () => (this.showMap = !this.showMap), style: {
                  "--color": "rgba(var(--ion-text-color-rgb), 0.5)",
                }, fill: "outline", size: "small" },
                "Map",
                h("ion-icon", { slot: "end", name: "map" })),
              h("ion-button", { style: {
                  "--color": "rgba(var(--ion-text-color-rgb), 0.5)",
                }, onClick: (event) => this.fireenjinTrigger.emit({
                  event,
                  name: "filter",
                  type: "status",
                  payload: {
                    control: {
                      name: "status",
                      label: "Status",
                      icon: "help-circle",
                      multiple: true,
                      options: getStatusOptions(),
                      value: this.selectedStatuses,
                    },
                  },
                }), fill: "outline", size: "small" },
                "Filter",
                h("ion-icon", { slot: "end", name: "funnel" })))),
          h("ion-card", { class: "list-card" },
            h("ion-list", null, myJobs.map((job) => (h("floodteam-item-job", { job: job }))))))),
      h("floodteam-navigation-footer", null),
    ];
  }
  static get is() { return "app-dashboard"; }
  static get originalStyleUrls() { return {
    "$": ["app-dashboard.css"]
  }; }
  static get styleUrls() { return {
    "$": ["app-dashboard.css"]
  }; }
  static get properties() { return {
    "db": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DatabaseService",
        "resolved": "DatabaseService",
        "references": {
          "DatabaseService": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "groupBy": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "group-by",
      "reflect": false,
      "defaultValue": "\"week\""
    }
  }; }
  static get states() { return {
    "unreadCount": {},
    "selectedStatuses": {},
    "totals": {},
    "options": {},
    "timeframe": {},
    "showMap": {}
  }; }
  static get events() { return [{
      "method": "fireenjinTrigger",
      "name": "fireenjinTrigger",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinTriggerInput",
        "resolved": "FireEnjinTriggerInput",
        "references": {
          "FireEnjinTriggerInput": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }, {
      "method": "fireenjinSubmit",
      "name": "fireenjinSubmit",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "FireEnjinSubmitEvent",
        "resolved": "FireEnjinSubmitEvent",
        "references": {
          "FireEnjinSubmitEvent": {
            "location": "import",
            "path": "@fireenjin/sdk"
          }
        }
      }
    }]; }
  static get listeners() { return [{
      "name": "fireenjinTrigger",
      "method": "onTrigger",
      "target": "body",
      "capture": false,
      "passive": false
    }, {
      "name": "ionChange",
      "method": "onChange",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
//# sourceMappingURL=app-dashboard.js.map