'use strict';

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

const index = require('./index-e0385008.js');
const hardwareBackButton = require('./overlays-83fe75fb.js');
const store = require('./store-8d1de66f.js');
const findUser = require('./findUser-c9cd5747.js');
const isAdmin = require('./isAdmin-536e5c3e.js');
require('./ionic-global-45d4a7f7.js');
require('./helpers-905b0d22.js');
require('./index-37fc02d8.js');
require('./getHandlebarsHelpers-1b368c8a.js');
require('./_commonjsHelpers-a5111d61.js');
require('./index-ba857994.js');
require('./localforage-af1b8c0e.js');

const appLocationCss = "";

const AppLocation = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.locationId = undefined;
    this.db = undefined;
    this.location = undefined;
    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.findUser(this.db, userId));
    }
    this.autoTieUsers = autoTieUsers;
  }
  async presentToast() {
    const toast = await hardwareBackButton.toastController.create({
      message: "The location has been updated.",
      duration: 2000,
    });
    toast.present();
  }
  async openUserSearch() {
    this.modalEl = await hardwareBackButton.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 (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    store.state.feedFetchData = {
      ...((store.state === null || store.state === void 0 ? void 0 : store.state.feedFetchData) || {}),
      locationId: this.locationId,
      myFeed: null,
    };
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    return [
      index.h("ion-content", { scrollEvents: true, class: "ion-padding" }, index.h("floodteam-title-bar", null, index.h("ion-router-link", { href: "/locations" }, "Locations")), index.h("ion-card", { class: "ion-padding" }, index.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 }, index.h("ion-list", null, index.h("ion-grid", null, index.h("ion-row", null, index.h("ion-col", null, index.h("ion-card", null, index.h("fireenjin-input-photo", { name: "photo", value: (_a = this.location) === null || _a === void 0 ? void 0 : _a.photo, type: "location" }), index.h("ion-item", null, index.h("ion-label", { class: "ion-text-center" }, "Photo")))), index.h("ion-col", null, index.h("ion-card", null, index.h("fireenjin-input-photo", { name: "bannerPhoto", value: (_b = this.location) === null || _b === void 0 ? void 0 : _b.bannerPhoto }), index.h("ion-item", null, index.h("ion-label", { class: "ion-text-center" }, "Banner")))), index.h("ion-col", null, index.h("ion-card", null, index.h("fireenjin-input-photo", { name: "teamPhoto", value: (_c = this.location) === null || _c === void 0 ? void 0 : _c.teamPhoto }), index.h("ion-item", null, index.h("ion-label", { class: "ion-text-center" }, "Team")))))), index.h("fireenjin-input", { "data-fill": true, name: "name", label: "Location Name", labelPosition: "stacked" }), index.h("fireenjin-select", { "data-fill": true, name: "type", label: "Location Type", labelPosition: "stacked", options: [
          {
            label: "Corporate",
            value: "corporate",
          },
          {
            label: "Franchise",
            value: "franchise",
          },
        ] }), index.h("fireenjin-input", { "data-fill": true, name: "email", type: "email", label: "E-Mail", labelPosition: "stacked" }), index.h("fireenjin-input", { "data-fill": true, name: "website", label: "Website", labelPosition: "stacked" }), index.h("fireenjin-input", { "data-fill": true, name: "phone", type: "phone", label: "Phone Number", labelPosition: "stacked" }), index.h("fireenjin-input-address", { "data-fill": true, name: "address", label: "Address", labelPosition: "stacked", googleMapsKey: (_e = (_d = store.state === null || store.state === void 0 ? void 0 : store.state.config) === null || _d === void 0 ? void 0 : _d.firebase) === null || _e === void 0 ? void 0 : _e.apiKey }), index.h("fireenjin-input-search-user", { "data-fill": true, label: "Owner", labelPosition: "stacked", name: "ownerId", autofocus: true, placeholder: "Search Users", mode: "popover" }), index.h("fireenjin-input", { "data-fill": true, name: "taxId", label: "Location EIN (Tax ID)", labelPosition: "stacked" }), index.h("fireenjin-select", { "data-fill": true, name: "defaultPayModelId", label: "Default Pay Model", endpoint: "listPayModels", limit: 100, dataPropsMap: {
          payModels: "results",
        } }), index.h("fireenjin-select-tags", { "data-fill": true, name: "tags", label: "Tags", labelPosition: "stacked", allowAdding: true, options: [] }), index.h("fireenjin-input", { "data-fill": true, name: "googleTagManagerId", label: "Google Tag Manager ID", labelPosition: "stacked" }), index.h("fireenjin-input", { "data-fill": true, name: "googleTagManagerCampaignId", label: "Google Tag Manager Campaign ID", labelPosition: "stacked" }), index.h("fireenjin-input", { "data-fill": true, name: "googleTagManagerPhoneId", label: "Google TM Phone Campaign ID", labelPosition: "stacked" }), isAdmin.isAdmin() && (index.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) || [] })), index.h("ion-item", null, index.h("ion-label", null, index.h("h2", null, "Auto-Tie Users"), index.h("fireenjin-chip-bar", null, (this.autoTieUsers || []).map((user) => (index.h("ion-chip", null, (user === null || user === void 0 ? void 0 : user.firstName) || "", " ", (user === null || user === void 0 ? void 0 : user.lastName) || "", index.h("ion-icon", { name: "remove-circle", onClick: () => this.removeUser(user === null || user === void 0 ? void 0 : user.id) })))))), index.h("ion-button", { slot: "end", fill: "clear", shape: "round", color: "primary", onClick: () => this.openUserSearch() }, "Add User", index.h("ion-icon", { name: "add-circle", slot: "end" }))))))),
      index.h("floodteam-navigation-footer", null),
    ];
  }
};
AppLocation.style = appLocationCss;

exports.app_location = AppLocation;
