import { h, } from "@stencil/core";
import getRoleOptions from "../../../helpers/getRoleOptions";
import state from "../../../store";
export class SelectRole {
  constructor() {
    this.name = "role";
    this.value = undefined;
    this.jobId = undefined;
    this.userId = undefined;
    this.disabled = false;
  }
  onTrigger(event) {
    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;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.name) !== "select" ||
      this.selectRoleEl !== ((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.el) ||
      ((_d = (_c = event === null || event === void 0 ? void 0 : event.detail) === null || _c === void 0 ? void 0 : _c.payload) === null || _d === void 0 ? void 0 : _d.value) === this.value)
      return;
    let warnRemoveSignatures = false;
    for (const efile of ((_e = state === null || state === void 0 ? void 0 : state.job) === null || _e === void 0 ? void 0 : _e.efiles) || []) {
      if (warnRemoveSignatures)
        break;
      for (const signature of (efile === null || efile === void 0 ? void 0 : efile.signatures) || []) {
        if ((((_f = signature === null || signature === void 0 ? void 0 : signature.createdBy) === null || _f === void 0 ? void 0 : _f.id) &&
          ((_g = signature === null || signature === void 0 ? void 0 : signature.createdBy) === null || _g === void 0 ? void 0 : _g.id) ===
            ((_m = (_l = (_k = (_j = (_h = event === null || event === void 0 ? void 0 : event.detail) === null || _h === void 0 ? void 0 : _h.event) === null || _j === void 0 ? void 0 : _j.detail) === null || _k === void 0 ? void 0 : _k.payload) === null || _l === void 0 ? void 0 : _l.control) === null || _m === void 0 ? void 0 : _m.userId)) ||
          ((_s = (_r = [...(_q = (_p = (_o = signature === null || signature === void 0 ? void 0 : signature.createdBy) === null || _o === void 0 ? void 0 : _o._key) === null || _p === void 0 ? void 0 : _p.path) === null || _q === void 0 ? void 0 : _q.segments]) === null || _r === void 0 ? void 0 : _r.pop) === null || _s === void 0 ? void 0 : _s.call(_r)) ===
            ((_x = (_w = (_v = (_u = (_t = event === null || event === void 0 ? void 0 : event.detail) === null || _t === void 0 ? void 0 : _t.event) === null || _u === void 0 ? void 0 : _u.detail) === null || _v === void 0 ? void 0 : _v.payload) === null || _w === void 0 ? void 0 : _w.control) === null || _x === void 0 ? void 0 : _x.userId)) {
          warnRemoveSignatures = true;
          break;
        }
      }
    }
    if (warnRemoveSignatures &&
      confirm("This will delete all signatures captured on contracts for this user, are you sure?")) {
      this.fireenjinSubmit.emit({
        endpoint: "removeSignaturesFromEfiles",
        data: {
          jobId: this.jobId,
          userId: this.userId,
        },
      });
    }
    else if (warnRemoveSignatures) {
      return;
    }
    this.value = ((_z = (_y = event === null || event === void 0 ? void 0 : event.detail) === null || _y === void 0 ? void 0 : _y.payload) === null || _z === void 0 ? void 0 : _z.value) || null;
    this.fireenjinSubmit.emit({
      endpoint: "addUserToJob",
      data: {
        jobId: this.jobId,
        userId: this.userId,
        role: this.value,
      },
    });
  }
  render() {
    return (h("ion-chip", { onClick: (event) => {
        if (this.disabled)
          return;
        event.preventDefault();
        event.stopPropagation();
        this.fireenjinTrigger.emit({
          event,
          name: "filter",
          type: "role",
          el: this.selectRoleEl,
          payload: {
            control: {
              name: "role",
              label: "Role",
              icon: "person-circle",
              options: getRoleOptions(),
              value: this.value,
              userId: this.userId,
            },
          },
        });
      } }, h("small", { class: "ion-text-capitalize" }, this.value || "No Role")));
  }
  static get is() { return "floodteam-select-role"; }
  static get properties() {
    return {
      "name": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "name",
        "reflect": false,
        "defaultValue": "\"role\""
      },
      "value": {
        "type": "string",
        "mutable": true,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "value",
        "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
      },
      "disabled": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "disabled",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get events() {
    return [{
        "method": "fireenjinTrigger",
        "name": "fireenjinTrigger",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "FireEnjinTriggerInput",
          "resolved": "FireEnjinTriggerInput",
          "references": {
            "FireEnjinTriggerInput": {
              "location": "import",
              "path": "@fireenjin/sdk"
            }
          }
        }
      }, {
        "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 elementRef() { return "selectRoleEl"; }
  static get listeners() {
    return [{
        "name": "fireenjinTrigger",
        "method": "onTrigger",
        "target": "document",
        "capture": false,
        "passive": false
      }];
  }
}
