import { r as registerInstance, h, H as Host } from './index-ecdfb761.js';
import { T as TextField } from './index-150b6a59.js';
import { M as ModifierTrimmer } from './ModifierTrimmer-2f77f917.js';
import { V as ValidatorLength } from './ValidatorLength-f56a7c14.js';
import { p as prod } from './services-68676f67.js';
import { D as DamgaApi } from './damga-api-dbb2bd94.js';
import { A as APIBase } from './apiBase-648a161d.js';
import './FieldGenerator-3ae403b2.js';
import './BaseField-0b01dc2c.js';
import './InterceptorHandler-6474bcf4.js';
import './data-transfer-field-2f305919.js';
import './filter-cbaa3d7d.js';

class DasDamgaApi {
  constructor(endpoint, defaultHeaders = {}, defaultQueryParams = {}) {
    // region proxy analyses
    this.getProxyAnalyses = (domains, startDate, endDate, timeInterval) => this.api.get(`analyses`, { domains: domains.join(','), startDate, endDate, timeInterval });
    // endregion
    // region logs
    this.getLogs = (domain, startDate, endDate) => this.api.get(`logs`, { domain, startDate, endDate });
    this.api = new APIBase(endpoint, defaultHeaders);
    this.api.axios.defaults.params = defaultQueryParams;
  }
}

const DevingenProductPage = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.showEditProductForm = false;
    this.showDeleteConfirmation = false;
    this.fetchProduct = async () => {
      const { id } = this.match.params;
      this.product = await this.apiDevingen.getProduct(id).catch((e) => this.error = e);
    };
    this.renderHeader = () => {
      var _a;
      return (h("div", { class: "container" }, h("div", { class: "row" }, h("div", { class: "col d-flex align-items-center pt-2 pb-3" }, h("h2", { class: "m-0" }, (_a = this.product) === null || _a === void 0 ? void 0 : _a.name), h("dvn-icon-button", { iconName: "uil uil-cog", scale: "small", view: "flat", onClick: () => this.showEditProductForm = true })))));
    };
    this.renderEditProductForm = () => {
      if (!this.showEditProductForm) {
        return null;
      }
      return (h("dvn-dialog", { shown: true, onDismiss: () => this.showEditProductForm = false }, h("doldurma-form", { fields: [
          new TextField('name', 'Name').addInterceptor('onSubmit', new ValidatorLength(2, 32))
            .addInterceptor('onSubmit', new ModifierTrimmer())
            .setValue(this.product.name)
            .require(),
        ], onSubmit: this.onSubmitEditProductForm, submitButtonLabel: "Update", extraButtons: [{
            label: 'Delete',
            tint: 'danger',
            view: 'flat',
            onClick: () => {
              this.showDeleteConfirmation = true;
            },
          }], formErrors: this.updateError ? ['Update failed.'] : undefined })));
    };
    this.renderDeleteProductConfirmation = () => {
      var _a;
      if (!this.showDeleteConfirmation) {
        return null;
      }
      return (h("dvn-dialog", { shown: true, onDismiss: () => {
          this.showDeleteConfirmation = false;
          this.deleteError = undefined;
        }, title: "Deletion" }, h("p", null, "Are you sure to delete this product? This action cannot be undone."), this.deleteError &&
        h("p", { class: "text-danger" }, ((_a = this.deleteError.data) === null || _a === void 0 ? void 0 : _a.error) === 'product-has-target-groups'
          ? 'You need to delete all domains, load balancers and target groups inside the product before deleting it.'
          : 'Delete failed.'), h("div", { slot: "actions" }, h("dvn-button", { view: "flat", onClick: () => {
          this.showDeleteConfirmation = false;
          this.deleteError = undefined;
        } }, "Cancel"), h("dvn-button", { tint: "danger", onClick: this.onConfirmDeleteProduct }, "Yes, Delete"))));
    };
    this.onSubmitEditProductForm = (event) => {
      const { values } = event.detail;
      this.apiDevingen.updateProduct(this.product._id, Object.assign(Object.assign({}, this.product), { name: values.name }))
        .then(() => {
        this.showEditProductForm = false;
        this.fetchProduct();
      })
        .catch(error => this.updateError = error);
    };
    this.onConfirmDeleteProduct = () => {
      this.apiDevingen.deleteProduct(this.product._id)
        .then(() => location.pathname = '/')
        .catch(error => this.deleteError = error);
    };
  }
  componentWillLoad() {
    this.fetchProduct();
  }
  render() {
    const { apiData, apiDevingen, apiKimlik, product, products, workspaceOwnerships } = this;
    if (!product) {
      return (h("div", { class: "d-flex justify-content-center pt-3" }, h("dvn-progress", { indefinite: true })));
    }
    let Tag = `devingen-product-root-${product.type}`;
    if (product.type === 'damga') {
      return (h(Host, null, this.renderHeader(), h("damga-home", { apiEndpoint: prod.damga, apiHeaders: {
          Authorization: apiKimlik.api.getDefaults().headers['Authorization']
        }, apiQueryParams: {
          productId: product._id
        }, extraTabs: [
          // {
          //   key: 'logs',
          //   label: 'Logs',
          //   render: () => <damga-domain-logs
          //     apiDamga={
          //       new DamgaApi(
          //         services.damga,
          //         { Authorization: (apiKimlik as any).api.getDefaults().headers['Authorization'] },
          //         { productId: product._id },
          //       )
          //     }
          //     apiServicesDamga={
          //       new DasDamgaApi(
          //         services.dasDamga,
          //         { Authorization: (apiKimlik as any).api.getDefaults().headers['Authorization'] },
          //         { productId: product._id },
          //       )
          //     }
          //   />
          // },
          {
            key: 'dashboard',
            label: 'Dashboard',
            render: () => h("damga-dashboard", { apiDamga: new DamgaApi(prod.damga, { Authorization: apiKimlik.api.getDefaults().headers['Authorization'] }, { productId: product._id }), apiServicesDamga: new DasDamgaApi(prod.dasDamga, { Authorization: apiKimlik.api.getDefaults().headers['Authorization'] }, { productId: product._id }) })
          }
        ] }), this.renderEditProductForm(), this.renderDeleteProductConfirmation()));
    }
    return (h(Host, null, this.renderHeader(), this.renderEditProductForm(), this.renderDeleteProductConfirmation(), h(Tag, { apiData: apiData, apiDevingen: apiDevingen, apiHeaders: {
        Authorization: apiKimlik.api.getDefaults().headers['Authorization']
      }, product: product, products: products, workspaceOwnerships: workspaceOwnerships })));
  }
};

export { DevingenProductPage as devingen_product_page };
