import { r as registerInstance, h, H as Host } from './index-ecdfb761.js';
import { A as APIBase } from './apiBase-6015f41f.js';
import './data-transfer-field-2f305919.js';
import './BaseField-0b01dc2c.js';
import './filter-cbaa3d7d.js';

class SepetApi {
  constructor(endpoint, defaultHeaders = {}, defaultQueryParams = {}) {
    // region buckets
    this.createBucket = (data) => this.api.post('buckets', data);
    this.getBuckets = () => this.api.get('buckets');
    this.getBucket = (id) => this.api.get(`buckets/${id}`);
    this.getBucketVersionList = (id) => this.api.get(`buckets/${id}/versions`);
    this.updateBucket = (id, data) => this.api.put(`buckets/${id}`, data);
    this.deleteBucket = (id) => this.api.delete(`buckets/${id}`);
    // endregion
    // region files
    this.uploadFile = (domain, path, data) => {
      const formData = new FormData();
      formData.append(path, data);
      return this.api.post(domain, formData);
    };
    this.getFileList = (domain, path, version) => {
      const headers = {};
      if (version) {
        headers['bucket-version'] = version;
      }
      return this.api.get(`${domain}/${path}`, {}, headers);
    };
    this.deleteFileOrFolder = (domain, path) => this.api.delete(`${domain}/${path}`);
    this.api = new APIBase(endpoint, defaultHeaders);
    this.api.axios.defaults.params = defaultQueryParams;
  }
}

const SepetHome = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.showFileExplorer = false;
    this.fileExplorerPath = '';
    this.showDeleteConfirmation = false;
    this.showItemForm = false;
    this.listKey = 1;
    this.onClickAddNewItem = () => this.showItemForm = true;
    this.onConfirmDeleteItem = () => {
      this.isDeletePending = true;
      this.apiSepet.deleteBucket(this.selectedItem._id)
        .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.apiSepet = new SepetApi(this.apiEndpoint, this.apiHeaders, this.apiQueryParams);
  }
  onAction(event) {
    event.stopImmediatePropagation();
    const action = event.detail;
    switch (action.name) {
      case 'open': {
        this.showFileExplorer = true;
        this.selectedItem = action.params.item;
        this.fileExplorerPath = this.selectedItem.folder;
        break;
      }
      case 'delete': {
        this.showDeleteConfirmation = true;
        this.selectedItem = action.params.item;
        break;
      }
      case 'edit': {
        this.showItemForm = true;
        this.selectedItem = action.params.item;
        break;
      }
    }
  }
  render() {
    const { apiSepet } = this;
    if (this.showFileExplorer) {
      return (h(Host, { class: "d-flex flex-column", style: { minHeight: 'calc(100vh - 50px)' } }, h("sepet-file-explorer", { apiSepet: apiSepet, item: this.selectedItem, onDismissFileExplorer: () => {
          this.showFileExplorer = false;
          this.selectedItem = undefined;
          this.fileExplorerPath = '';
        } })));
    }
    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 mb-3" }, h("div", { class: "flex-grow-1" }, "Buckets"), h("dvn-button", { onClick: this.onClickAddNewItem, leaned: true }, "New Bucket"))))), h("sepet-bucket-list", { key: this.listKey, apiSepet: apiSepet }), this.showItemForm &&
      h("dvn-dialog", { shown: true, onDismiss: this.dismissItemFormDialog }, h("sepet-bucket-form", { apiSepet: apiSepet, 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")))));
  }
};

export { SepetHome as sepet_home };
