import { r as registerInstance, d as createEvent, B as Build, h } from './index-31949dc5.js';
import { s as state } from './store-cca1abc6.js';
import { g as getStatusOptions } from './getStatusOptions-6f716ba9.js';
import { i as isAdmin } from './isAdmin-f11962b3.js';
import { i as isInternalTeam } from './isInternalTeam-02ab69ba.js';
import { g as getInvoiceTypes } from './getInvoiceTypes-8686f775.js';
import './index-afdf7f54.js';
import './getHandlebarsHelpers-ae9bfed4.js';
import './_commonjsHelpers-44457d8d.js';
import './index-e6b3332e.js';
import './localforage-394b59fa.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) {
    registerInstance(this, hostRef);
    this.fireenjinTrigger = 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());
    const filter = (_a = state === null || state === void 0 ? void 0 : 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 = state === null || state === void 0 ? void 0 : state.claims) === null || _b === void 0 ? void 0 : _b.locationId) || ((_d = (_c = state === null || state === void 0 ? void 0 : state.profile) === null || _c === void 0 ? void 0 : _c.location) === null || _d === void 0 ? void 0 : _d.id)),
      userId: this.userId
        ? this.userId
        : isInternalTeam()
          ? null
          : (_e = state === null || state === void 0 ? void 0 : state.session) === null || _e === void 0 ? void 0 : _e.uid,
      serviceTypes: null,
      // tags: filter?.tags || null,
    };
  }
  componentWillLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : 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 [
      h("ion-content", { class: "truck-background" }, this.showMap && (h("fireenjin-map", { googleMapsKey: (_c = (_b = state === null || state === void 0 ? void 0 : 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 })), 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" }, h("fireenjin-search-bar", { style: {
          width: "100%",
        }, paginationEl: this.paginationEl, filters: [
          {
            name: "status",
            label: "Status",
            icon: "help-circle",
            multiple: true,
            options: getStatusOptions(),
            value: (_d = this.fetchData) === null || _d === void 0 ? void 0 : _d.status,
          },
          {
            name: "serviceTypes",
            label: "Type",
            icon: "reader",
            multiple: true,
            options: 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()
            ? [
              {
                name: "locationId",
                label: "Location",
                icon: "business",
                options: ((_f = state === null || state === void 0 ? void 0 : state.locations) === null || _f === void 0 ? void 0 : _f.length)
                  ? state.locations.map((location) => ({
                    label: location.name,
                    value: location.id,
                  }))
                  : [],
                value: ((_g = this.fetchData) === null || _g === void 0 ? void 0 : _g.locationId) || null,
              },
            ]
            : []),
        ] }), h("ion-button", { fill: "clear", onClick: () => (this.showMap = !this.showMap) }, h("ion-icon", { slot: "icon-only", name: "map" }))), h("fireenjin-pagination", { ref: (el) => (this.paginationEl = el), disableVirtualScroll: true, listEl: ({ result: job }) => (h("floodteam-item-job", { job: job })), endpoint: "listJobs", limit: 20, disableFetch: !((_h = state === null || state === void 0 ? void 0 : 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" })),
      h("floodteam-navigation-footer", null, h("floodteam-title-bar", { disableUnderline: true, back: "/dashboard" }, h("ion-breadcrumbs", null, h("ion-breadcrumb", { href: "/jobs" }, h("ion-icon", { class: "ft-icon-style", slot: "start", name: "water" }), "Jobs")), h("ion-buttons", { slot: "end" }, isAdmin() && (h("ion-button", { title: "Import CSV", onClick: () => this.fireenjinTrigger.emit({
          name: "import",
          type: "jobs",
        }) }, h("ion-icon", { slot: "icon-only", name: "cloud-upload" }))), isAdmin() && (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,
              });
            }),
          });
        } }, h("ion-icon", { slot: "icon-only", name: "cloud-download" }))), h("ion-button", { slot: "end", fill: "solid", color: "primary", href: "/jobs/refer" }, "Add")))),
    ];
  }
};
AppJobList.style = appJobListCss;

export { AppJobList as app_job_list };
