'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-3e5ae956.js');
const store = require('./store-c1e254ab.js');
const getStatusOptions = require('./getStatusOptions-64f3eefc.js');
const isAdmin = require('./isAdmin-c4ca01b5.js');
const isInternalTeam = require('./isInternalTeam-d5394e8d.js');
const getInvoiceTypes = require('./getInvoiceTypes-6482a668.js');
require('./index-b020f7d8.js');
require('./getHandlebarsHelpers-93797155.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./localforage-af1b8c0e.js');

const appJobListCss = "app-job-list fireenjin-map{background:var(--ion-color-light-shade);border-radius:15px;display:block;margin:10px auto 0;overflow:hidden;max-width:calc(100% - 30px)}app-job-list #pin-bar .pins{top:112px !important}";

const AppJobList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.userId = undefined;
    this.user = {};
    this.jobs = [];
    this.query = undefined;
    this.paginationEl = undefined;
    this.selectedIds = [];
    this.selectedJobs = [];
    this.selectAll = false;
    this.fetchData = undefined;
    this.showMap = false;
  }
  onSearchInput(event) {
    this.query = event.target.value;
  }
  async onSuccess(event) {
    var _a, _b;
    if (["addJob", "editJob"].includes((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint))
      setTimeout(() => this.paginationEl.getResults(), 200);
    if (((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.endpoint) !== "listJobs")
      return;
    setTimeout(() => {
      var _a;
      this.jobs = ((_a = this.paginationEl) === null || _a === void 0 ? void 0 : _a.results) || [];
    }, 200);
  }
  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")
      return;
    // Start Here replace key and value
    const key = (_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;
    const value = (_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;
    this.fetchData = { ...this.fetchData, [key]: value };
    // this.refreshFetchData();
  }
  toggleSelect(event, job) {
    event.stopPropagation();
    event.preventDefault();
    this.selectedJobs = this.selectedJobs.includes(job)
      ? this.selectedJobs.filter((selectedJob) => job.id !== selectedJob.id)
      : [...new Set([...this.selectedJobs, job])];
    this.selectedIds = this.selectedIds.includes(job.id)
      ? this.selectedIds.filter((selectedId) => job.id !== selectedId)
      : [...new Set([...this.selectedIds, job.id])];
  }
  toggleSelectAll() {
    var _a, _b, _c;
    if ((_a = this.selectedIds) === null || _a === void 0 ? void 0 : _a.length) {
      this.selectedJobs = [];
      this.selectedIds = [];
      this.selectAll = false;
    }
    else {
      if ((_c = (_b = this.paginationEl) === null || _b === void 0 ? void 0 : _b.results) === null || _c === void 0 ? void 0 : _c.length) {
        this.selectedJobs = this.paginationEl.results;
        this.selectedIds = this.paginationEl.results.map((result) => result.id);
      }
    }
  }
  refreshFetchData() {
    var _a, _b, _c, _d, _e;
    console.log(isInternalTeam.isInternalTeam());
    const filter = (_a = store.state === null || store.state === void 0 ? void 0 : store.state.filters) === null || _a === void 0 ? void 0 : _a[location === null || location === void 0 ? void 0 : location.pathname];
    this.fetchData = {
      status: (filter === null || filter === void 0 ? void 0 : filter.status) ||
        (this.userId
          ? null
          : [
            "book",
            "evaluate",
            "claim",
            "mitigate",
            "monitor",
            "review",
            "imported",
          ]),
      locationId: (filter === null || filter === void 0 ? void 0 : filter.locationId) ||
        (this.userId
          ? null
          : ((_b = store.state === null || store.state === void 0 ? void 0 : store.state.claims) === null || _b === void 0 ? void 0 : _b.locationId) || ((_d = (_c = store.state === null || store.state === void 0 ? void 0 : store.state.profile) === null || _c === void 0 ? void 0 : _c.location) === null || _d === void 0 ? void 0 : _d.id)),
      userId: this.userId
        ? this.userId
        : isInternalTeam.isInternalTeam()
          ? null
          : (_e = store.state === null || store.state === void 0 ? void 0 : store.state.session) === null || _e === void 0 ? void 0 : _e.uid,
      serviceTypes: null,
      // tags: filter?.tags || null,
    };
  }
  componentWillLoad() {
    if (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    this.refreshFetchData();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const markers = (((_a = this.jobs) === null || _a === void 0 ? void 0 : _a.length) ? this.jobs : []).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,
        },
      });
    });
    return [
      index.h("ion-content", { class: "truck-background" }, this.showMap && (index.h("fireenjin-map", { googleMapsKey: (_c = (_b = store.state === null || store.state === void 0 ? void 0 : store.state.config) === null || _b === void 0 ? void 0 : _b.firebase) === null || _c === void 0 ? void 0 : _c.apiKey, ref: (el) => (this.mapEl = el), markers: markers })), index.h("div", { style: {
          display: "flex",
          alignItems: "center",
          background: `linear-gradient(0deg, rgba(255, 255, 255,0) 37%, rgba(255, 255, 255,0.2) 92%)`,
        }, class: "ion-padding" }, index.h("fireenjin-search-bar", { style: {
          width: "100%",
        }, paginationEl: this.paginationEl, filters: [
          {
            name: "status",
            label: "Status",
            icon: "help-circle",
            multiple: true,
            options: getStatusOptions.getStatusOptions(),
            value: (_d = this.fetchData) === null || _d === void 0 ? void 0 : _d.status,
          },
          {
            name: "serviceTypes",
            label: "Type",
            icon: "reader",
            multiple: true,
            options: getInvoiceTypes.getInvoiceTypes(),
            value: (_e = this.fetchData) === null || _e === void 0 ? void 0 : _e.serviceTypes,
          },
          // {
          //   name: "tags",
          //   label: "Tags",
          //   icon: "pricetags",
          //   multiple: true,
          //   value: filter?.tags,
          //   options: [
          //     {
          //       label: "Imported",
          //       value: "imported",
          //     },
          //   ],
          // },
          ...(isAdmin.isAdmin()
            ? [
              {
                name: "locationId",
                label: "Location",
                icon: "business",
                options: ((_f = store.state === null || store.state === void 0 ? void 0 : store.state.locations) === null || _f === void 0 ? void 0 : _f.length)
                  ? store.state.locations.map((location) => ({
                    label: location.name,
                    value: location.id,
                  }))
                  : [],
                value: ((_g = this.fetchData) === null || _g === void 0 ? void 0 : _g.locationId) || null,
              },
            ]
            : []),
        ] }), index.h("ion-button", { fill: "clear", onClick: () => (this.showMap = !this.showMap) }, index.h("ion-icon", { slot: "icon-only", name: "map" }))), index.h("fireenjin-pagination", { ref: (el) => (this.paginationEl = el), disableVirtualScroll: true, listEl: ({ result: job }) => (index.h("floodteam-item-job", { job: job })), endpoint: "listJobs", limit: 20, disableFetch: !((_h = store.state === null || store.state === void 0 ? void 0 : store.state.session) === null || _h === void 0 ? void 0 : _h.uid) || !this.fetchData, fetchParams: { withPayables: true }, fetchData: this.fetchData, resultsKey: "jobs", query: this.query, removeDuplicates: true, name: "jobs" })),
      index.h("floodteam-navigation-footer", null, index.h("floodteam-title-bar", { disableUnderline: true, back: "/dashboard" }, index.h("ion-breadcrumbs", null, index.h("ion-breadcrumb", { href: "/jobs" }, index.h("ion-icon", { class: "ft-icon-style", slot: "start", name: "water" }), "Jobs")), index.h("ion-buttons", { slot: "end" }, isAdmin.isAdmin() && (index.h("ion-button", { title: "Import CSV", onClick: () => this.fireenjinTrigger.emit({
          name: "import",
          type: "jobs",
        }) }, index.h("ion-icon", { slot: "icon-only", name: "cloud-upload" }))), isAdmin.isAdmin() && (index.h("ion-button", { title: "Export CSV", onClick: () => {
          this.fireenjinTrigger.emit({
            name: "export",
            payload: (this.jobs || []).map((job) => {
              var _a;
              return ({
                id: (job === null || job === void 0 ? void 0 : job.id) || null,
                title: (job === null || job === void 0 ? void 0 : job.title) || null,
                address: ((_a = job === null || job === void 0 ? void 0 : job.address) === null || _a === void 0 ? void 0 : _a.full) || null,
              });
            }),
          });
        } }, index.h("ion-icon", { slot: "icon-only", name: "cloud-download" }))), index.h("ion-button", { slot: "end", fill: "solid", color: "primary", href: "/jobs/refer" }, "Add")))),
    ];
  }
};
AppJobList.style = appJobListCss;

exports.app_job_list = AppJobList;
