import { Component, Fragment, h, Host, Listen, Prop, State } from '@stencil/core';
import { DamgaApi } from '../../api/damga-api';
export class DamgaHome {
  constructor() {
    this.selectedTab = 'domains';
    this.showDeleteConfirmation = false;
    this.showItemForm = false;
    this.listKey = 1;
    this.onClickAddNewItem = () => this.showItemForm = true;
    this.onConfirmDeleteItem = () => {
      let promise;
      if (this.selectedTab === 'load-balancers') {
        promise = this.apiDamga.deleteLoadBalancer(this.selectedItem._id);
      }
      else if (this.selectedTab === 'target-groups') {
        promise = this.apiDamga.deleteTargetGroup(this.selectedItem._id);
      }
      else if (this.selectedTab === 'domains') {
        promise = this.apiDamga.deleteDomain(this.selectedItem._id);
      }
      this.isDeletePending = true;
      promise === null || promise === void 0 ? void 0 : promise.then(this.invalidate).catch(this.onDeleteFail).finally(() => this.isDeletePending = false);
    };
    this.onDeleteFail = (error) => {
      var _a, _b, _c, _d;
      if (((_a = error.data) === null || _a === void 0 ? void 0 : _a.error) === 'target-group-used-in-load-balancers') {
        this.deleteErrorMessage = `Target group is being used by load balancers ${(_b = error.data.loadBalancers) === null || _b === void 0 ? void 0 : _b.map(lb => lb.name).join(', ')}.`;
      }
      else if (((_c = error.data) === null || _c === void 0 ? void 0 : _c.error) === 'load-balancer-used-in-domains') {
        this.deleteErrorMessage = `Load balancer is being used by domains ${(_d = error.data.domains) === null || _d === void 0 ? void 0 : _d.map(d => d.domain).join(', ')}.`;
      }
    };
    this.dismissItemFormDialog = () => {
      this.showItemForm = false;
      this.selectedItem = undefined;
    };
    this.dismissDeleteConfirmationDialog = () => {
      this.showDeleteConfirmation = false;
      this.selectedItem = undefined;
      this.deleteErrorMessage = undefined;
    };
    this.invalidate = () => {
      this.listKey += 1;
      this.showItemForm = false;
      this.showDeleteConfirmation = false;
      this.selectedItem = undefined;
      this.deleteErrorMessage = undefined;
    };
  }
  componentWillLoad() {
    this.apiDamga = new DamgaApi(this.apiEndpoint, this.apiHeaders, this.apiQueryParams);
  }
  onAction(event) {
    event.stopImmediatePropagation();
    const action = event.detail;
    switch (action.name) {
      case 'delete': {
        this.showDeleteConfirmation = true;
        this.selectedItem = action.params.item;
        break;
      }
      case 'edit': {
        this.showItemForm = true;
        this.selectedItem = action.params.item;
        break;
      }
    }
  }
  render() {
    const { apiDamga, selectedTab } = this;
    const tabs = [
      { label: 'Domains', value: 'domains' },
      { label: 'Load Balancers', value: 'load-balancers' },
      { label: 'Target Groups', value: 'target-groups' },
    ];
    const newItemLabels = {
      'domains': 'Domain',
      'load-balancers': 'Load Balancer',
      'target-groups': 'Target Group',
    };
    return (h(Host, { class: "d-flex flex-column", style: { minHeight: 'calc(100vh - 50px)' } },
      h("div", { class: "container" },
        h("div", { class: "row" },
          h("div", { class: "col-sm-12" },
            h("div", { class: "d-flex align-items-center" },
              h("div", { class: "flex-grow-1" },
                h("dvn-menu", { orientation: "horizontal" }, tabs.map(({ label, value }) => (h("dvn-menu-item", { highlighted: value === selectedTab, onClick: () => this.selectedTab = value }, label))))),
              newItemLabels[selectedTab] &&
                h("dvn-button", { onClick: this.onClickAddNewItem },
                  "New ",
                  newItemLabels[selectedTab]))))),
      selectedTab === 'domains' &&
        h(Fragment, null,
          h("damga-domain-list", { key: this.listKey, apiDamga: apiDamga }),
          this.showItemForm &&
            h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog },
              h("damga-domain-form", { apiDamga: apiDamga, onChange: this.invalidate, item: this.selectedItem }))),
      selectedTab === 'load-balancers' &&
        h(Fragment, null,
          h("damga-load-balancer-list", { key: this.listKey, apiDamga: apiDamga }),
          this.showItemForm &&
            h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog },
              h("damga-load-balancer-form", { apiDamga: apiDamga, onChange: this.invalidate, item: this.selectedItem }))),
      selectedTab === 'target-groups' &&
        h(Fragment, null,
          h("damga-target-group-list", { key: this.listKey, apiDamga: apiDamga }),
          this.showItemForm &&
            h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog },
              h("damga-target-group-form", { apiDamga: apiDamga, onChange: this.invalidate, item: this.selectedItem }))),
      this.showDeleteConfirmation &&
        h("dvn-dialog", { shown: true, onDismiss: this.dismissDeleteConfirmationDialog, title: "Deletion" },
          h("p", null, "Are you sure to delete this item? This action cannot be undone."),
          this.deleteErrorMessage &&
            h("p", { class: "text-danger" }, this.deleteErrorMessage),
          h("div", { slot: "actions" },
            h("dvn-button", { view: "flat", onClick: this.dismissDeleteConfirmationDialog }, "Cancel"),
            h("dvn-button", { tint: "danger", disabled: this.isDeletePending, progressing: this.isDeletePending, onClick: this.onConfirmDeleteItem }, "Yes, Delete")))));
  }
  static get is() { return "damga-home"; }
  static get properties() { return {
    "apiEndpoint": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "api-endpoint",
      "reflect": false
    },
    "apiHeaders": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "api-headers",
      "reflect": false
    },
    "apiQueryParams": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "api-query-params",
      "reflect": false
    }
  }; }
  static get states() { return {
    "selectedTab": {},
    "showDeleteConfirmation": {},
    "showItemForm": {},
    "selectedItem": {},
    "listKey": {},
    "deleteErrorMessage": {},
    "isDeletePending": {}
  }; }
  static get listeners() { return [{
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
