'use strict';

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

const index = require('./index-1141a245.js');
const kimlikApi = require('./kimlik-api-d3c479a3.js');
require('./apiBase-e4ebaf8b.js');
require('./data-transfer-field-e8417d5f.js');
require('./BaseField-381327a9.js');
require('./filter-d5040f65.js');

const tabs = [
  { label: 'Users', value: 'users' },
  { label: 'API Keys', value: 'api-keys' },
  { label: 'SAML Configs', value: 'saml-configs' },
];
const newItemLabels = {
  // 'users': 'User',
  'api-keys': 'API Key',
  'saml-configs': 'SAML Config',
};
const KimlikHome = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.selectedTab = 'saml-configs';
    this.showDeleteConfirmation = false;
    this.showItemForm = false;
    this.listKey = 1;
    this.onClickAddNewItem = () => this.showItemForm = true;
    this.onConfirmDeleteItem = () => {
      let promise;
      if (this.selectedTab === 'api-keys') {
        promise = this.apiKimlik.deleteApiKey(this.selectedItem._id);
      }
      else if (this.selectedTab === 'saml-configs') {
        promise = this.apiKimlik.deleteSAMLConfig(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) => {
      // if (error.data?.error === 'target-group-used-in-load-balancers') {
      //   this.deleteErrorMessage = `Target group is being used by load balancers ${error.data.loadBalancers?.map(lb => lb.name).join(', ')}.`;
      // } else if (error.data?.error === 'load-balancer-used-in-domains') {
      //   this.deleteErrorMessage = `Load balancer is being used by domains ${error.data.domains?.map(d => d.domain).join(', ')}.`;
      // }
      this.deleteErrorMessage = 'Unexpected error.';
    };
    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.apiKimlik = new kimlikApi.KimlikApiImpl(this.apiEndpoint, this.apiHeaders);
  }
  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 { apiKimlik, selectedTab } = this;
    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 === 'users' &&
      index.h(index.Fragment, null, index.h("kimlik-user-list", { key: this.listKey, apiKimlik: apiKimlik })), selectedTab === 'api-keys' &&
      index.h(index.Fragment, null, index.h("kimlik-api-key-list", { key: this.listKey, apiKimlik: apiKimlik }), this.showItemForm &&
        index.h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog }, index.h("kimlik-api-key-form", { apiKimlik: apiKimlik, onChange: this.invalidate, item: this.selectedItem }))), selectedTab === 'saml-configs' &&
      index.h(index.Fragment, null, index.h("kimlik-saml-config-list", { key: this.listKey, apiKimlik: apiKimlik }), this.showItemForm &&
        index.h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog }, index.h("kimlik-saml-config-form", { apiKimlik: apiKimlik, 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.kimlik_home = KimlikHome;
