import { Build, h, } from "@stencil/core";
import { toastController } from "@ionic/core";
import state from "../../../store";
export class appMaterial {
  constructor() {
    this.materialId = undefined;
    this.material = {};
  }
  async onSuccess(event) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    if (((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint) === "findMaterial") {
      this.material = ((_c = (_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.material) || null;
    }
    else if (((_d = event === null || event === void 0 ? void 0 : event.detail) === null || _d === void 0 ? void 0 : _d.endpoint) === "editMaterial") {
      (await toastController.create({
        message: "Material saved successfully!",
        duration: 3000,
      })).present();
    }
    else if (((_e = event === null || event === void 0 ? void 0 : event.detail) === null || _e === void 0 ? void 0 : _e.endpoint) === "addMaterial" &&
      this.materialId === "new") {
      const routerEl = document.querySelector("ion-router");
      this.materialId = ((_h = (_g = (_f = event === null || event === void 0 ? void 0 : event.detail) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.addMaterial) === null || _h === void 0 ? void 0 : _h.id) || "new";
      if (routerEl)
        routerEl.push(`/materials/${this.materialId}`);
    }
  }
  async fetchMaterial() {
    this.fireenjinFetch.emit({
      endpoint: "findMaterial",
      params: {
        id: this.materialId,
      },
    });
  }
  componentDidLoad() {
    if (!(Build === null || Build === void 0 ? void 0 : Build.isBrowser))
      return;
    this.fetchMaterial();
  }
  render() {
    var _a, _b, _c;
    return [
      h("ion-content", { class: "ion-padding" }, h("floodteam-title-bar", null, h("ion-router-link", { href: "/materials" }, "Materials")), h("ion-card", { style: {
          overflow: "visible",
        } }, h("fireenjin-form", { resetButton: "Clear", resetButtonColor: "medium", ref: (el) => (this.formEl = el), endpoint: this.materialId === "new" ? "addMaterial" : "editMaterial", documentId: this.materialId === "new" ? null : this.materialId }, h("ion-grid", null, h("ion-row", null, h("ion-col", { size: "10" }, h("fireenjin-input", { "data-fill": true, labelPosition: "stacked", type: "text", name: "name", label: "Name", value: (_a = this.material) === null || _a === void 0 ? void 0 : _a.name, required: true }))))))),
      h("floodteam-navigation-footer", null, h("floodteam-title-bar", { back: "/materials" }, h("ion-breadcrumbs", null, h("ion-breadcrumb", { href: "/materials" }, h("ion-icon", { class: "ft-icon-style", slot: "start", name: "construct" }), "Materials"), h("ion-breadcrumb", { onClick: () => {
          var _a;
          return this.fireenjinTrigger.emit({
            name: "pin",
            payload: {
              icon: "flash",
              href: `/materials/${this.materialId}`,
              name: ((_a = this.material) === null || _a === void 0 ? void 0 : _a.name) || "Add a Material",
            },
          });
        } }, h("ion-icon", { color: ((_b = ((state === null || state === void 0 ? void 0 : state.pins) || [])
          .map((pin) => pin === null || pin === void 0 ? void 0 : pin.href)) === null || _b === void 0 ? void 0 : _b.includes(`/materials/${this.materialId}`))
          ? "primary"
          : null, slot: "start", name: "pin" }), ((_c = this.material) === null || _c === void 0 ? void 0 : _c.name) || "Edit Material")))),
      ,
    ];
  }
  static get is() { return "app-material"; }
  static get originalStyleUrls() {
    return {
      "$": ["app-material.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["app-material.css"]
    };
  }
  static get properties() {
    return {
      "materialId": {
        "type": "any",
        "mutable": true,
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "material-id",
        "reflect": false
      }
    };
  }
  static get states() {
    return {
      "material": {}
    };
  }
  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": "FireEnjinSubmitEvent",
          "resolved": "FireEnjinSubmitEvent",
          "references": {
            "FireEnjinSubmitEvent": {
              "location": "import",
              "path": "@fireenjin/sdk"
            }
          }
        }
      }, {
        "method": "fireenjinTrigger",
        "name": "fireenjinTrigger",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "FireEnjinTriggerInput",
          "resolved": "FireEnjinTriggerInput<any>",
          "references": {
            "FireEnjinTriggerInput": {
              "location": "import",
              "path": "@fireenjin/sdk"
            }
          }
        }
      }];
  }
  static get listeners() {
    return [{
        "name": "fireenjinSuccess",
        "method": "onSuccess",
        "target": "document",
        "capture": false,
        "passive": false
      }];
  }
}
