import { r as registerInstance, d as createEvent, B as Build, h } from './index-bcae2ade.js';
import { t as toastController, m as modalController } from './overlays-a73ddb6f.js';
import { s as state } from './store-fdd7f338.js';
import { f as findUser } from './findUser-ae1ac9ce.js';
import { i as isAdmin } from './isAdmin-dd43c884.js';
import './ionic-global-0135b4d6.js';
import './helpers-7e937ffa.js';
import './index-571e7e3a.js';
import './getHandlebarsHelpers-ae9bfed4.js';
import './_commonjsHelpers-44457d8d.js';
import './index-e6b3332e.js';
import './localforage-394b59fa.js';

const appLocationCss = "";

const AppLocation = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinFetch = createEvent(this, "fireenjinFetch", 7);
    this.autoTieUsers = [];
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    if (event.detail.endpoint === "addLocation") {
      const routerEl = document.querySelector("ion-router");
      if (!routerEl || !((_c = (_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.addLocation) === null || _c === void 0 ? void 0 : _c.id))
        return;
      routerEl.push(`/locations/${event.detail.data.addLocation.id}`);
    }
    else if (event.detail.endpoint === "editLocation") {
      this.presentToast();
    }
    else if (((_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.endpoint) === "findLocation") {
      this.location = ((_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.location) || null;
      this.setAutoTieUsers();
    }
    else if (((_g = event === null || event === void 0 ? void 0 : event.detail) === null || _g === void 0 ? void 0 : _g.endpoint) === "upload") {
      this.location[(_h = event === null || event === void 0 ? void 0 : event.detail) === null || _h === void 0 ? void 0 : _h.name] =
        ((_l = (_k = (_j = event === null || event === void 0 ? void 0 : event.detail) === null || _j === void 0 ? void 0 : _j.data) === null || _k === void 0 ? void 0 : _k.upload) === null || _l === void 0 ? void 0 : _l.url) || null;
      this.location = { ...this.location };
    }
  }
  onSubmit(event) {
    var _a, _b, _c, _d;
    console.log(event, this.location);
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "selectUsers") {
      const autoTieUserIds = [
        ...(((_b = this.location) === null || _b === void 0 ? void 0 : _b.autoTieUserIds) || []),
        ...(((_d = (_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.userId) || []),
      ];
      this.location = {
        ...this.location,
        autoTieUserIds,
      };
      this.setAutoTieUsers();
    }
  }
  async setAutoTieUsers() {
    var _a;
    const autoTieUsers = [];
    for (const userId of ((_a = this.location) === null || _a === void 0 ? void 0 : _a.autoTieUserIds) || []) {
      autoTieUsers.push(await findUser(this.db, userId));
    }
    this.autoTieUsers = autoTieUsers;
  }
  async presentToast() {
    const toast = await toastController.create({
      message: "The location has been updated.",
      duration: 2000,
    });
    toast.present();
  }
  async openUserSearch() {
    this.modalEl = await modalController.create({
      cssClass: "modal-search-users",
      component: "modal-search-users",
      componentProps: {},
    });
    this.modalEl.present();
  }
  removeUser(id) {
    var _a;
    if (!confirm("Are you sure you want to remove this auto-tie user?"))
      return;
    this.location = {
      ...this.location,
      autoTieUserIds: (((_a = this.location) === null || _a === void 0 ? void 0 : _a.autoTieUserIds) || []).filter((userId) => userId !== id),
    };
    this.setAutoTieUsers();
  }
  componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    state.feedFetchData = {
      ...((state === null || state === void 0 ? void 0 : state.feedFetchData) || {}),
      locationId: this.locationId,
      myFeed: null,
    };
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    return [
      h("ion-content", { scrollEvents: true, class: "ion-padding" }, h("floodteam-title-bar", null, h("ion-router-link", { href: "/locations" }, "Locations")), h("ion-card", { class: "ion-padding" }, h("fireenjin-form", { fetch: this.locationId === "new" ? false : "findLocation", fetchKey: "location", endpoint: this.locationId === "new" ? "addLocation" : "editLocation", formData: this.location, documentId: this.locationId === "new" ? null : this.locationId, filterData: [
          "autoTieUserIds",
          "serviceTypes",
          "name",
          "phone",
          "email",
          "address",
          "ownerId",
          "defaultPayModelId",
          "website",
          "googleTagManagerId",
          "googleTagManagerCampaignId",
          "googleTagManagerPhoneId",
          "tags",
          "bannerPhoto",
          "photo",
          "teamPhoto",
          "taxId",
        ], beforeSubmit: (data) => ({ ...this.location, ...data }), resetButton: null }, h("ion-list", null, h("ion-grid", null, h("ion-row", null, h("ion-col", null, h("ion-card", null, h("fireenjin-input-photo", { name: "photo", value: (_a = this.location) === null || _a === void 0 ? void 0 : _a.photo, type: "location" }), h("ion-item", null, h("ion-label", { class: "ion-text-center" }, "Photo")))), h("ion-col", null, h("ion-card", null, h("fireenjin-input-photo", { name: "bannerPhoto", value: (_b = this.location) === null || _b === void 0 ? void 0 : _b.bannerPhoto }), h("ion-item", null, h("ion-label", { class: "ion-text-center" }, "Banner")))), h("ion-col", null, h("ion-card", null, h("fireenjin-input-photo", { name: "teamPhoto", value: (_c = this.location) === null || _c === void 0 ? void 0 : _c.teamPhoto }), h("ion-item", null, h("ion-label", { class: "ion-text-center" }, "Team")))))), h("fireenjin-input", { "data-fill": true, name: "name", label: "Location Name", labelPosition: "stacked" }), h("fireenjin-input", { "data-fill": true, name: "email", type: "email", label: "E-Mail", labelPosition: "stacked" }), h("fireenjin-input", { "data-fill": true, name: "website", label: "Website", labelPosition: "stacked" }), h("fireenjin-input", { "data-fill": true, name: "phone", type: "phone", label: "Phone Number", labelPosition: "stacked" }), h("fireenjin-input-address", { "data-fill": true, name: "address", label: "Address", labelPosition: "stacked", googleMapsKey: (_e = (_d = state === null || state === void 0 ? void 0 : state.config) === null || _d === void 0 ? void 0 : _d.firebase) === null || _e === void 0 ? void 0 : _e.apiKey }), h("fireenjin-input-search-user", { "data-fill": true, label: "Owner", labelPosition: "stacked", name: "ownerId", autofocus: true, placeholder: "Search Users", mode: "popover", required: true }), h("fireenjin-input", { "data-fill": true, name: "taxId", label: "Location EIN (Tax ID)", labelPosition: "stacked" }), h("fireenjin-select", { "data-fill": true, name: "defaultPayModelId", label: "Default Pay Model", endpoint: "listPayModels", limit: 100, dataPropsMap: {
          payModels: "results",
        } }), h("fireenjin-select-tags", { "data-fill": true, name: "tags", label: "Tags", labelPosition: "stacked", allowAdding: true, options: [] }), h("fireenjin-input", { "data-fill": true, name: "googleTagManagerId", label: "Google Tag Manager ID", labelPosition: "stacked" }), h("fireenjin-input", { "data-fill": true, name: "googleTagManagerCampaignId", label: "Google Tag Manager Campaign ID", labelPosition: "stacked" }), h("fireenjin-input", { "data-fill": true, name: "googleTagManagerPhoneId", label: "Google TM Phone Campaign ID", labelPosition: "stacked" }), isAdmin() && (h("floodteam-select-job-service-types", { locationId: this.locationId, value: ((_f = this === null || this === void 0 ? void 0 : this.location) === null || _f === void 0 ? void 0 : _f.serviceTypes) || [] })), h("ion-item", null, h("ion-label", null, h("h2", null, "Auto-Tie Users"), h("fireenjin-chip-bar", null, (this.autoTieUsers || []).map((user) => (h("ion-chip", null, (user === null || user === void 0 ? void 0 : user.firstName) || "", " ", (user === null || user === void 0 ? void 0 : user.lastName) || "", h("ion-icon", { name: "remove-circle", onClick: () => this.removeUser(user === null || user === void 0 ? void 0 : user.id) })))))), h("ion-button", { slot: "end", fill: "clear", shape: "round", color: "primary", onClick: () => this.openUserSearch() }, "Add User", h("ion-icon", { name: "add-circle", slot: "end" }))))))),
      h("floodteam-navigation-footer", null),
    ];
  }
};
AppLocation.style = appLocationCss;

export { AppLocation as app_location };
