'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-ce58ddc5.js');
const apiBase = require('./apiBase-b14a8285.js');
require('./data-transfer-field-0ce05c1a.js');
require('./BaseField-d019ade6.js');
require('./filter-d5040f65.js');

class DamgaApi {
  constructor(endpoint, defaultHeaders = {}, defaultQueryParams = {}) {
    // region domains
    this.createDomain = (data) => this.api.post(`domains`, data);
    this.getDomains = () => this.api.get(`domains`);
    this.updateDomain = (id, data) => this.api.put(`domains/${id}`, data);
    this.deleteDomain = (id) => this.api.delete(`domains/${id}`);
    // endregion
    // region load-balancers
    this.createLoadBalancer = (data) => this.api.post(`load-balancers`, data);
    this.getLoadBalancers = () => this.api.get(`load-balancers`);
    this.updateLoadBalancer = (id, data) => this.api.put(`load-balancers/${id}`, data);
    this.deleteLoadBalancer = (id) => this.api.delete(`load-balancers/${id}`);
    // endregion
    // region target-groups
    this.createTargetGroup = (data) => this.api.post(`target-groups`, data);
    this.getTargetGroups = () => this.api.get(`target-groups`);
    this.updateTargetGroup = (id, data) => this.api.put(`target-groups/${id}`, data);
    this.deleteTargetGroup = (id) => this.api.delete(`target-groups/${id}`);
    // endregion
    this.getDnsRecord = (domain) => this.api.post(`tools/dns-lookup`, { domain });
    this.api = new apiBase.APIBase(endpoint, defaultHeaders);
    this.api.axios.defaults.params = defaultQueryParams;
  }
}

let DamgaHome = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    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 (index.h(index.Host, { class: "d-flex flex-column", style: { minHeight: 'calc(100vh - 50px)' } }, index.h("div", { class: "container" }, index.h("div", { class: "row" }, index.h("div", { class: "col-sm-12" }, index.h("div", { class: "d-flex align-items-center" }, index.h("div", { class: "flex-grow-1" }, index.h("dvn-menu", { orientation: "horizontal" }, tabs.map(({ label, value }) => (index.h("dvn-menu-item", { highlighted: value === selectedTab, onClick: () => this.selectedTab = value }, label))))), newItemLabels[selectedTab] &&
      index.h("dvn-button", { onClick: this.onClickAddNewItem }, "New ", newItemLabels[selectedTab]))))), selectedTab === 'domains' &&
      index.h(index.Fragment, null, index.h("damga-domain-list", { key: this.listKey, apiDamga: apiDamga }), this.showItemForm &&
        index.h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog }, index.h("damga-domain-form", { apiDamga: apiDamga, onChange: this.invalidate, item: this.selectedItem }))), selectedTab === 'load-balancers' &&
      index.h(index.Fragment, null, index.h("damga-load-balancer-list", { key: this.listKey, apiDamga: apiDamga }), this.showItemForm &&
        index.h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog }, index.h("damga-load-balancer-form", { apiDamga: apiDamga, onChange: this.invalidate, item: this.selectedItem }))), selectedTab === 'target-groups' &&
      index.h(index.Fragment, null, index.h("damga-target-group-list", { key: this.listKey, apiDamga: apiDamga }), this.showItemForm &&
        index.h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog }, index.h("damga-target-group-form", { apiDamga: apiDamga, onChange: this.invalidate, item: this.selectedItem }))), this.showDeleteConfirmation &&
      index.h("dvn-dialog", { shown: true, onDismiss: this.dismissDeleteConfirmationDialog, title: "Deletion" }, index.h("p", null, "Are you sure to delete this item? This action cannot be undone."), this.deleteErrorMessage &&
        index.h("p", { class: "text-danger" }, this.deleteErrorMessage), index.h("div", { slot: "actions" }, index.h("dvn-button", { view: "flat", onClick: this.dismissDeleteConfirmationDialog }, "Cancel"), index.h("dvn-button", { tint: "danger", disabled: this.isDeletePending, progressing: this.isDeletePending, onClick: this.onConfirmDeleteItem }, "Yes, Delete")))));
  }
};

exports.damga_home = DamgaHome;
