import { toastController } from "@ionic/core";
import { Build, h, } from "@stencil/core";
import state from "../../../store";
export class AppSign {
  constructor() {
    this.templateId = undefined;
    this.jobId = undefined;
    this.userId = undefined;
    this.loading = false;
    this.signed = false;
    this.template = undefined;
  }
  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: {
          jobId: this.jobId,
          signatures: [
            {
              data: await event.target.toDataURL(),
              userId: this.userId || ((_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, _c, _d;
    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;
    }
    else if (((_b = event.detail) === null || _b === void 0 ? void 0 : _b.endpoint) === "findTemplate") {
      this.template = ((_d = (_c = event.detail) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.template)
        ? event.detail.data.template
        : null;
    }
  }
  async fetchJob() {
    if (!this.jobId)
      return;
    this.fireenjinFetch.emit({
      endpoint: "findJob",
      params: {
        id: this.jobId,
        withPayments: false,
        withUsers: true,
        withSites: false,
        withPayModel: false,
        withPhotos: false,
        withEfiles: false,
        withPayables: false,
        withReadings: false,
      },
    });
  }
  async presentToast() {
    const toast = await toastController.create({
      message: `File(s) added successfully to Efile!`,
      duration: 2000,
    });
    toast.present();
  }
  componentDidLoad() {
    if (Build.isBrowser) {
      this.fetchJob();
      setTimeout(() => {
        this.forceResize.emit({});
      }, 1000);
    }
  }
  render() {
    var _a;
    return [
      h("ion-content", { class: "ion-padding" }, h("floodteam-title-bar", { back: ((_a = state === null || state === void 0 ? void 0 : state.session) === null || _a === void 0 ? void 0 : _a.uid) ? `/jobs/${this.jobId}` : "/" }, "E-Sign"), 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", { class: {
          "is-loading": this.loading,
        } }, h("floodteam-template", { style: {
          padding: "60px 2em",
        }, templateId: this.templateId, disableFrame: true, disablePosition: true, data: {
          timestamp: new Date().toISOString(),
          job: state.job,
          user: state === null || state === void 0 ? void 0 : state.profile,
        } }), h("floodteam-signature-pad", { ref: (el) => (this.signaturePadEl = el) })))),
      h("floodteam-navigation-footer", null),
    ];
  }
  static get is() { return "app-sign"; }
  static get originalStyleUrls() {
    return {
      "$": ["app-sign.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["app-sign.css"]
    };
  }
  static get properties() {
    return {
      "templateId": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "template-id",
        "reflect": false
      },
      "jobId": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "job-id",
        "reflect": false
      },
      "userId": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "user-id",
        "reflect": false
      }
    };
  }
  static get states() {
    return {
      "loading": {},
      "signed": {},
      "template": {}
    };
  }
  static get events() {
    return [{
        "method": "fireenjinFetch",
        "name": "fireenjinFetch",
        "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": "any",
          "resolved": "any",
          "references": {}
        }
      }, {
        "method": "forceResize",
        "name": "forceResize",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }];
  }
  static get listeners() {
    return [{
        "name": "ionChange",
        "method": "onChange",
        "target": undefined,
        "capture": false,
        "passive": false
      }, {
        "name": "fireenjinSuccess",
        "method": "onSuccess",
        "target": "body",
        "capture": false,
        "passive": false
      }];
  }
}
