import { Build, h, } from "@stencil/core";
import state from "../../../store";
import getParameter from "../../../helpers/getParameter";
import { toastController } from "@ionic/core";
import isInternalTeam from "../../../helpers/auth/isInternalTeam";
export class AppProfile {
  constructor() {
    this.user = state.profile;
    this.to = undefined;
  }
  onRouteChange(event) {
    const code = getParameter("code");
    if (Build.isBrowser && event.detail.to === "/stripe/success" && code) {
      setTimeout(async () => {
        var _a;
        if (!((_a = state === null || state === void 0 ? void 0 : state.session) === null || _a === void 0 ? void 0 : _a.uid))
          return;
        try {
          await this.fireenjinSubmit.emit({
            endpoint: "createStripeOAuthToken",
            data: {
              id: state.session.uid,
              code,
            },
          });
        }
        catch (err) {
          console.log("Error connecting to Stripe Account!", err);
          document.querySelector("ion-router").push("/profile");
        }
      }, 2000);
    }
  }
  onReset(event) {
    var _a;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "profile" &&
      confirm("Are you sure you want to logout?")) {
      this.floodteamLogout.emit();
    }
  }
  async onSuccess(event) {
    if (event.detail.name === "invite") {
      this.to = null;
      (await toastController.create({
        message: `Successfully sent invite(s).`,
        duration: 3000,
      })).present();
    }
  }
  onBeforeSubmit(formData) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    return {
      templateId: "93t0jXODOEc6D1kJGOD5",
      from: "info@thefloodteam.com",
      emails: ((_b = (_a = formData === null || formData === void 0 ? void 0 : formData.to) === null || _a === void 0 ? void 0 : _a.split) === null || _b === void 0 ? void 0 : _b.call(_a)) || [],
      data: {
        firstName: "Partner",
        referrer: {
          id: (_c = state === null || state === void 0 ? void 0 : state.session) === null || _c === void 0 ? void 0 : _c.uid,
          name: `${((_d = state === null || state === void 0 ? void 0 : state.profile) === null || _d === void 0 ? void 0 : _d.firstName) || ""} ${((_e = state === null || state === void 0 ? void 0 : state.profile) === null || _e === void 0 ? void 0 : _e.lastName) || ""}`,
          title: ((_f = state === null || state === void 0 ? void 0 : state.profile) === null || _f === void 0 ? void 0 : _f.type) || "Mitigation Specialist",
          phone: (_g = state === null || state === void 0 ? void 0 : state.profile) === null || _g === void 0 ? void 0 : _g.phone,
          location: (_j = (_h = state === null || state === void 0 ? void 0 : state.profile) === null || _h === void 0 ? void 0 : _h.location) === null || _j === void 0 ? void 0 : _j.name,
        },
      },
    };
  }
  componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4;
    return (h("div", null, h("ion-card", null, h("ion-card-content", { class: "ion-padding" }, h("fireenjin-input-photo", { color: "secondary", name: "profilePhoto", type: "user", documentId: (_a = state === null || state === void 0 ? void 0 : state.profile) === null || _a === void 0 ? void 0 : _a.id, fallback: `https://avatars.dicebear.com/api/initials/${((_d = (_c = (_b = state === null || state === void 0 ? void 0 : state.profile) === null || _b === void 0 ? void 0 : _b.firstName) === null || _c === void 0 ? void 0 : _c.charAt) === null || _d === void 0 ? void 0 : _d.call(_c, 0)) || ""}${((_g = (_f = (_e = state === null || state === void 0 ? void 0 : state.profile) === null || _e === void 0 ? void 0 : _e.lastName) === null || _f === void 0 ? void 0 : _f.charAt) === null || _g === void 0 ? void 0 : _g.call(_f, 0)) || ""}.svg`, value: (_h = state === null || state === void 0 ? void 0 : state.profile) === null || _h === void 0 ? void 0 : _h.photo }), h("fireenjin-form", { name: "profile", endpoint: "editUser", documentId: (_j = state === null || state === void 0 ? void 0 : state.session) === null || _j === void 0 ? void 0 : _j.uid, resetButton: "Logout", filterData: [
        "slackHandle",
        "firstName",
        "lastName",
        "address",
        "phone",
        "billingAddress",
        "photo",
        "email",
        "slackHandle",
      ] }, h("ion-list", null, isInternalTeam() && (h("fireenjin-input", { name: "slackHandle", label: "Slack Handle", labelPosition: "stacked", placeholder: "What is your slack handle?", value: (_k = state.profile) === null || _k === void 0 ? void 0 : _k.slackHandle })), h("fireenjin-input", { name: "firstName", label: "First Name", labelPosition: "stacked", placeholder: "What is your first name?", value: (_l = state.profile) === null || _l === void 0 ? void 0 : _l.firstName }), h("fireenjin-input", { name: "lastName", label: "Last Name", labelPosition: "stacked", placeholder: "What is your last name?", value: (_m = state.profile) === null || _m === void 0 ? void 0 : _m.lastName }), h("fireenjin-input", { name: "email", label: "Email Address", labelPosition: "stacked", placeholder: "What is your primary email address?", value: (_o = state.profile) === null || _o === void 0 ? void 0 : _o.email }), h("fireenjin-input", { name: "phone", type: "phone", label: "Phone Number", labelPosition: "stacked", placeholder: "What is your primary phone number?", value: (_p = state.profile) === null || _p === void 0 ? void 0 : _p.phone }), h("fireenjin-input-address", { name: "address", labelPosition: "stacked", label: "Address", placeholder: "What is your home address?", googleMapsKey: (_r = (_q = state === null || state === void 0 ? void 0 : state.config) === null || _q === void 0 ? void 0 : _q.firebase) === null || _r === void 0 ? void 0 : _r.apiKey, value: (_s = state.profile) === null || _s === void 0 ? void 0 : _s.address }), h("fireenjin-input-address", { name: "billingAddress", labelPosition: "stacked", label: "Billing Address", placeholder: "What is your billing address?", googleMapsKey: (_u = (_t = state === null || state === void 0 ? void 0 : state.config) === null || _t === void 0 ? void 0 : _t.firebase) === null || _u === void 0 ? void 0 : _u.apiKey, value: (_v = state.profile) === null || _v === void 0 ? void 0 : _v.billingAddress }))))), h("floodteam-title-bar", { icon: "logo-usd" }, h("ion-text", null, "Payment Info")), ((_w = state === null || state === void 0 ? void 0 : state.profile) === null || _w === void 0 ? void 0 : _w.isSetup) ? (h("floodteam-payment-methods", { methods: state.paymentMethods || [], stripeKey: ((_x = state.profile) === null || _x === void 0 ? void 0 : _x.isFranchise)
        ? (_0 = (_z = (_y = state.config) === null || _y === void 0 ? void 0 : _y.franchise) === null || _z === void 0 ? void 0 : _z.stripe) === null || _0 === void 0 ? void 0 : _0.key
        : (_2 = (_1 = state.config) === null || _1 === void 0 ? void 0 : _1.stripe) === null || _2 === void 0 ? void 0 : _2.key, userId: (_3 = state.profile) === null || _3 === void 0 ? void 0 : _3.id })) : (h("ion-card", null, h("ion-grid", null, h("ion-row", null, h("ion-col", { style: { maxWidth: "200px", padding: "15px" } }, h("ion-icon", { src: "/assets/svg/ft-icon.svg", style: {
        width: "100%",
        display: "block",
        height: "100%",
        minHeight: "100px",
        fill: "var(--ion-text-color)",
      } })), h("ion-col", { class: "ion-padding-horizontal" }, h("h2", { style: { color: "var(--ion-text-color)", marginTop: "8px" } }, "Your account is almost ready!"), h("p", { style: { padding: "10px 0" } }, "Please use the \"Setup Payments\" button below in order to finish setting up your account."), h("ion-button", { class: "ion-float-right", href: "#", onClick: () => this.fireenjinSubmit.emit({
        endpoint: "setupPayments",
      }) }, "Setup Payments", h("ion-icon", { slot: "end", name: "logo-usd" }))))))), h("floodteam-title-bar", { icon: "share" }, h("ion-text", null, "My Referral Link")), h("ion-card", null, h("ion-card-content", { class: "ion-padding" }, h("p", null, "Use one of the buttons below to invite users to The Flood Team App. Once a user logs in and finishes their onboarding they will be tied to you in the system."), h("br", null), h("fireenjin-form", { hideControls: true, name: "invite", endpoint: "sendTemplateEmail", disableEnterButton: true, beforeSubmit: this.onBeforeSubmit.bind(this) }, h("ion-grid", null, h("ion-row", null, h("ion-col", null, h("fireenjin-input", { name: "to", placeholder: "Comma separated list of emails", value: this.to }))), h("ion-row", null, h("ion-col", null, h("ion-button", { size: "default", expand: "block", fill: "clear", onClick: () => {
        var _a;
        return this.floodteamShare.emit({
          data: {
            url: `${state.config.url}?referredBy=${(_a = state.profile) === null || _a === void 0 ? void 0 : _a.id}`,
            text: "Hey, check out this new water remediation app from The Flood Team.",
            title: "Flood Team App",
          },
        });
      } }, "Share Link", h("ion-icon", { name: "share-social", slot: "start" }))), h("ion-col", null, h("ion-button", { size: "default", expand: "block", type: "submit" }, "Send", h("ion-icon", { name: "send", slot: "end" })))))))), h("ion-grid", null, h("ion-row", null, h("ion-col", { size: "7" }, h("ion-button", { fill: "clear", onClick: () => this.forceUpdate.emit({}) }, h("ion-label", null, h("h4", null, "v", ((_4 = state === null || state === void 0 ? void 0 : state.manifest) === null || _4 === void 0 ? void 0 : _4.version) || "Unknown"), h("small", null, "Check for Updates")))), h("ion-col", { size: "5" }, h("ion-button", { class: "theme-button", fill: "clear", onClick: () => (state.darkMode = !(state === null || state === void 0 ? void 0 : state.darkMode)) }, h("ion-label", null, "Theme"), h("ion-icon", { slot: "end", name: (state === null || state === void 0 ? void 0 : state.darkMode) ? "moon" : "sunny" }), h("ion-icon", { slot: "end", name: !(state === null || state === void 0 ? void 0 : state.darkMode) ? "moon" : "sunny" })))))));
  }
  static get is() { return "floodteam-profile"; }
  static get originalStyleUrls() {
    return {
      "$": ["app-profile.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["app-profile.css"]
    };
  }
  static get states() {
    return {
      "user": {},
      "to": {}
    };
  }
  static get events() {
    return [{
        "method": "floodteamLogout",
        "name": "floodteamLogout",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }, {
        "method": "floodteamShare",
        "name": "floodteamShare",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }, {
        "method": "forceUpdate",
        "name": "forceUpdate",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }, {
        "method": "fireenjinSubmit",
        "name": "fireenjinSubmit",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "FireEnjinSubmitEvent",
          "resolved": "FireEnjinSubmitEvent",
          "references": {
            "FireEnjinSubmitEvent": {
              "location": "import",
              "path": "@fireenjin/sdk"
            }
          }
        }
      }];
  }
  static get listeners() {
    return [{
        "name": "ionRouteDidChange",
        "method": "onRouteChange",
        "target": "body",
        "capture": false,
        "passive": false
      }, {
        "name": "fireenjinReset",
        "method": "onReset",
        "target": undefined,
        "capture": false,
        "passive": false
      }, {
        "name": "fireenjinSuccess",
        "method": "onSuccess",
        "target": "body",
        "capture": false,
        "passive": false
      }];
  }
}
