'use strict';

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

const index = require('./index-ee190ee0.js');
const hardwareBackButton = require('./overlays-6a64deff.js');
const store = require('./store-5a3b44dc.js');
const isAdmin = require('./isAdmin-95517cef.js');
const isInternalTeam = require('./isInternalTeam-134a4484.js');
const getCompanyOptions = require('./getCompanyOptions-d3ba5a4b.js');
const getUserRoleOptions = require('./getUserRoleOptions-63303496.js');
require('./ionic-global-542e0609.js');
require('./helpers-905b0d22.js');
require('./index-4901314f.js');
require('./getHandlebarsHelpers-8ef28401.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./localforage-af1b8c0e.js');

const appUserListCss = "app-users floodteam-snapshot-card{height:320px}app-users floodteam-snapshot-card .widget{height:320px}app-users floodteam-snapshot-card .widget__details{padding:0px}";

const AppUserList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    this.users = [];
    this.paginationEl = undefined;
    this.user = {};
    this.selectedUsers = [];
    this.selectedIds = [];
    this.selectedEmails = [];
    this.options = [];
    this.selectAll = false;
    this.fetchData = undefined;
  }
  onSearchInput(event) {
    if (!this.paginationEl)
      return;
    this.paginationEl.query = event.target.value;
  }
  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;
    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 };
  }
  onSuccess(event) {
    var _a;
    if (["addUser", "editUser"].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 (event.detail.endpoint === "bulkUpdateUsers") {
      this.paginationEl.getResults();
    }
  }
  async openPopover(event, user) {
    event.preventDefault();
    event.stopPropagation();
    const popover = await hardwareBackButton.popoverController.create({
      component: "popover-user",
      cssClass: "popover-user-wrapper",
      componentProps: { user },
      event,
    });
    return await popover.present();
  }
  async openEditUsersModal() {
    this.editUserModalEl = await hardwareBackButton.modalController.create({
      component: "modal-edit-users",
      cssClass: "modal-edit-users",
      componentProps: {
        users: this.selectedUsers,
      },
    });
    return await this.editUserModalEl.present();
  }
  async sendEmail(_event) {
    var _a, _b, _c, _d;
    this.sendEmailModalEl = await hardwareBackButton.modalController.create({
      component: "modal-send-email",
      cssClass: "modal-send-email-wrapper",
      componentProps: {
        isEblast: true,
        dryRun: false,
        to: this.selectedEmails.filter((email) => email === null || email === void 0 ? void 0 : email.includes("@")),
        templateFilter: (template) => { var _a, _b; return !((_b = (_a = template === null || template === void 0 ? void 0 : template.tags) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, "deleted")); },
        filters: {
          tags: ((_a = this.fetchData) === null || _a === void 0 ? void 0 : _a.tags) || null,
          locationId: ((_b = this.fetchData) === null || _b === void 0 ? void 0 : _b.locationId) || null,
          companyId: ((_c = this.fetchData) === null || _c === void 0 ? void 0 : _c.companyId) || null,
          role: ((_d = this.fetchData) === null || _d === void 0 ? void 0 : _d.role) || null,
        },
      },
    });
    return await this.sendEmailModalEl.present();
  }
  toggleSelect(event, user) {
    event.stopPropagation();
    event.preventDefault();
    this.selectedUsers = this.selectedUsers.includes(user)
      ? this.selectedUsers.filter((selectedUser) => user.id !== selectedUser.id)
      : [...new Set([...this.selectedUsers, user])];
    this.selectedEmails = this.selectedEmails.includes(user.email)
      ? this.selectedEmails.filter((selectedEmail) => user.email !== selectedEmail)
      : [...new Set([...this.selectedEmails, user.email])];
    this.selectedIds = this.selectedIds.includes(user.id)
      ? this.selectedIds.filter((selectedId) => user.id !== selectedId)
      : [...new Set([...this.selectedIds, user.id])];
  }
  toggleSelectAll() {
    var _a, _b, _c;
    if ((_a = this.selectedIds) === null || _a === void 0 ? void 0 : _a.length) {
      this.selectedUsers = [];
      this.selectedIds = [];
      this.selectedEmails = [];
      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.selectedUsers = this.paginationEl.results;
        this.selectedIds = this.paginationEl.results.map((result) => result.id);
        this.selectedEmails = this.paginationEl.results.map((result) => result.email);
      }
    }
  }
  refreshFetchData() {
    var _a, _b, _c, _d, _e, _f;
    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 = {
      roles: filter === null || filter === void 0 ? void 0 : filter.roles,
      status: filter === null || filter === void 0 ? void 0 : filter.status,
      companyId: filter === null || filter === void 0 ? void 0 : filter.companyId,
      locationId: (filter === null || filter === void 0 ? void 0 : filter.locationId) ||
        (((_b = this.user) === null || _b === void 0 ? void 0 : _b.id)
          ? null
          : ((_c = store.state === null || store.state === void 0 ? void 0 : store.state.claims) === null || _c === void 0 ? void 0 : _c.locationId) || ((_e = (_d = store.state === null || store.state === void 0 ? void 0 : store.state.profile) === null || _d === void 0 ? void 0 : _d.location) === null || _e === void 0 ? void 0 : _e.id)),
      userId: this.user
        ? this.user
        : isInternalTeam.isInternalTeam()
          ? null
          : (_f = store.state === null || store.state === void 0 ? void 0 : store.state.session) === null || _f === void 0 ? void 0 : _f.uid,
      tags: (filter === null || filter === void 0 ? void 0 : 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;
    return [
      index.h("ion-content", { class: "ion-padding" }, index.h("floodteam-title-bar", null, index.h("ion-router-link", { href: "/users" }, "Users"), index.h("ion-buttons", { slot: "end" }, index.h("ion-button", { fill: "solid", color: "primary", href: "/users/new" }, "Add"))), index.h("fireenjin-search-bar", { ref: (el) => (this.filterBarEl = el), paginationEl: this.paginationEl, filters: [
          {
            name: "role",
            label: "Role",
            icon: "lock-closed",
            value: (_a = this.fetchData) === null || _a === void 0 ? void 0 : _a.role,
            options: getUserRoleOptions.getUserRoleOptions(),
          },
          {
            name: "companyId",
            label: "Company",
            icon: "business",
            value: (_b = this.fetchData) === null || _b === void 0 ? void 0 : _b.companyId,
            options: getCompanyOptions.getCompanyOptions(),
          },
          {
            name: "tags",
            label: "Tags",
            icon: "pricetags",
            multiple: true,
            value: (_c = this.fetchData) === null || _c === void 0 ? void 0 : _c.tags,
            options: getCompanyOptions.getUserTagOptions(),
          },
          ...(isAdmin.isAdmin()
            ? [
              {
                name: "locationId",
                label: "Location",
                icon: "business",
                options: ((_d = store.state === null || store.state === void 0 ? void 0 : store.state.locations) === null || _d === void 0 ? void 0 : _d.length)
                  ? store.state.locations.map((location) => ({
                    label: location.name,
                    value: location.id,
                  }))
                  : [],
                value: ((_e = this.fetchData) === null || _e === void 0 ? void 0 : _e.locationId) || null,
              },
            ]
            : []),
        ] }), index.h("fireenjin-pagination", { name: "users", disableFetch: !isInternalTeam.isInternalTeam(), display: "list", results: this.users, ref: (el) => (this.paginationEl = el), disableVirtualScroll: true, listEl: ({ result: user }) => {
          var _a;
          return (index.h("ion-item", { href: `/users/${user.id}`, detail: true }, index.h("ion-checkbox", { slot: "start", checked: this.selectedIds.includes(user.id), onClick: (event) => this.toggleSelect(event, user) }), index.h("fireenjin-avatar", { src: user.photo, style: { marginRight: "15px" }, initials: `${(user === null || user === void 0 ? void 0 : user.firstName) ? user.firstName.charAt(0) : ""}${(user === null || user === void 0 ? void 0 : user.lastName) ? user.lastName.charAt(0) : ""}` }), index.h("ion-label", null, index.h("h4", null, user.firstName ? user.firstName : "", " ", user.lastName ? user.lastName : ""), ((_a = user === null || user === void 0 ? void 0 : user.tags) === null || _a === void 0 ? void 0 : _a.map)
            ? user.tags.map((tag) => index.h("ion-chip", null, tag))
            : null), index.h("ion-button", { fill: "clear", slot: "end", color: "medium", onClick: (event) => this.openPopover(event, user) }, index.h("ion-icon", { name: "ellipsis-vertical", slot: "icon-only" }))));
        }, endpoint: "listUsers", fetchData: this.fetchData, limit: 20, resultsKey: "users", removeDuplicates: true })),
      index.h("floodteam-navigation-footer", null, isAdmin.isAdmin() && (index.h("fireenjin-chip-bar", null, index.h("ion-chip", { onClick: () => this.fireenjinTrigger.emit({
          name: "import",
          type: "users",
          payload: {
            matchKey: "email",
            importTypes: {
              tags: "array",
              location: "reference"
            }
          }
        }) }, index.h("ion-icon", { class: "ft-icon-style", name: "cloud-upload" }), index.h("ion-label", null, "Import from CSV")), index.h("ion-chip", { onClick: () => {
          var _a, _b, _c, _d;
          const filters = { whereEqual: {} };
          if ((_a = this.fetchData) === null || _a === void 0 ? void 0 : _a.role)
            filters.whereEqual.role = this.fetchData.role || null;
          if ((_b = this.fetchData) === null || _b === void 0 ? void 0 : _b.tags)
            filters.whereArrayContainsAny = {
              tags: this.fetchData.tags || [],
            };
          if ((_c = this.fetchData) === null || _c === void 0 ? void 0 : _c.companyId)
            filters.whereEqual.companyId =
              this.fetchData.companyId || null;
          if ((_d = this.fetchData) === null || _d === void 0 ? void 0 : _d.locationId)
            filters.whereEqual.location = `~/locations/${this.fetchData.locationId}`;
          this.fireenjinTrigger.emit({
            name: "export",
            collection: "users",
            filters,
          });
        } }, index.h("ion-icon", { class: "ft-icon-style", name: "cloud-download" }), index.h("ion-label", null, "Export to CSV")), index.h("ion-chip", { onClick: () => this.toggleSelectAll() }, index.h("ion-icon", { name: "checkbox", class: "ft-icon-style" }), index.h("ion-label", null, ((_f = this.selectedIds) === null || _f === void 0 ? void 0 : _f.length) ? "Deselect All" : "Select All")), isInternalTeam.isInternalTeam() && (index.h("ion-chip", { onClick: () => {
          this.sendEmail({
            name: "Email",
            payload: (this.users || []).map((user) => {
              var _a;
              return ({
                id: (user === null || user === void 0 ? void 0 : user.id) || null,
                firstName: (user === null || user === void 0 ? void 0 : user.firstName) || null,
                lastName: (user === null || user === void 0 ? void 0 : user.lastName) || null,
                email: (user === null || user === void 0 ? void 0 : user.email) || null,
                address: ((_a = user === null || user === void 0 ? void 0 : user.address) === null || _a === void 0 ? void 0 : _a.full) || null,
                type: (user === null || user === void 0 ? void 0 : user.type) || null,
                phone: (user === null || user === void 0 ? void 0 : user.phone) || null,
              });
            }),
          });
        } }, index.h("ion-icon", { class: "ft-icon-style", name: "file-tray-full" }), index.h("ion-label", null, "Email")))))),
    ];
  }
};
AppUserList.style = appUserListCss;

exports.app_user_list = AppUserList;
