import { r as registerInstance, e as createEvent, h } from './index-e0adbd54.js';
import { t as toastController } from './overlays-b83a89c7.js';
import { s as state } from './store-4342e5d4.js';
import './ionic-global-3272af31.js';
import './helpers-06537f39.js';
import './index-96e8c812.js';
import './getHandlebarsHelpers-c1e27d99.js';
import './_commonjsHelpers-44457d8d.js';
import './index-e6b3332e.js';
import './localforage-394b59fa.js';

const ModalEsign = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.fireenjinSubmit = createEvent(this, "fireenjinSubmit", 7);
    this.floodteamCloseModal = createEvent(this, "floodteamCloseModal", 7);
    this.jobId = undefined;
    this.efileId = undefined;
    this.templateId = undefined;
    this.headerTitle = "E-sign";
    this.job = undefined;
    this.userSignId = undefined;
    this.loading = false;
    this.signed = false;
    this.isLoaded = false;
  }
  onModalPresent() {
    this.isLoaded = true;
  }
  async onChange(event) {
    var _a, _b;
    if (((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.name) === "signature") {
      const signature = await event.target.toDataURL();
      if (signature.length <= 10) {
        alert("Signature is required to submit!");
        return;
      }
      this.loading = true;
      this.fireenjinSubmit.emit({
        endpoint: "signContract",
        data: {
          efileId: this.efileId,
          jobId: this.jobId,
          user: this.userSignId,
          signatures: [
            {
              data: await event.target.toDataURL(),
              userId: this.userSignId || ((_b = state === null || state === void 0 ? void 0 : state.session) === null || _b === void 0 ? void 0 : _b.uid),
            },
          ],
          templateId: this.templateId,
        },
      });
    }
  }
  async onSuccess(event) {
    var _a, _b;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "signContract") {
      this.presentToast();
      this.loading = false;
      this.signed = true;
      this.floodteamCloseModal.emit();
    }
    else if (((_b = event.detail) === null || _b === void 0 ? void 0 : _b.endpoint) === "findJob") {
      this.loading = false;
    }
  }
  async presentToast() {
    const toast = await toastController.create({
      message: `File(s) added successfully to Efile!`,
      duration: 2000,
    });
    toast.present();
  }
  render() {
    setTimeout(() => {
      try {
        this.templateEl.querySelector(".contract-header").remove();
        this.templateEl.querySelector(".contract-footer").remove();
      }
      catch (e) {
        console.log(e);
      }
    }, 1000);
    return [
      h("ion-header", null, h("ion-toolbar", null, h("ion-title", null, this.headerTitle), h("ion-buttons", { slot: "end" }, h("ion-button", { onClick: () => this.floodteamCloseModal.emit() }, h("ion-icon", { slot: "icon-only", name: "close-circle-outline" }))))),
      h("ion-content", { class: {
          "ion-padding": true,
          "is-loading": this.loading,
        } }, this.signed ? (h("div", { style: {
          width: "90%",
          maxWidth: "500px",
          margin: "0 auto",
        } }, h("ion-icon", { name: "checkmark-circle", color: "success", style: {
          height: "150px",
          width: "150px",
          display: "block",
          margin: "30px auto",
        } }), h("h1", null, "Signature Submitted!"), h("p", null, "Thank you for your time, you can now close this browser tab."))) : (h("div", null, h("floodteam-template", { ref: (el) => (this.templateEl = el), class: "fullscreen", templateId: this.templateId, data: {
          timestamp: new Date().toISOString(),
          job: this.job,
          user: state === null || state === void 0 ? void 0 : state.profile,
        } })))),
      this.isLoaded && (h("floodteam-signature-pad", { class: {
          "is-loading": this.loading,
        }, ref: (el) => (this.signaturePadEl = el) })),
    ];
  }
};

export { ModalEsign as modal_esign };
