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 './FieldGenerator-3ae403b2.js';
import './BaseField-0b01dc2c.js';
import './InterceptorHandler-6474bcf4.js';

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-dropdown", { placement: "rightTop", trigger: "click", leaned: true }, h("dvn-icon-button", { iconName: "uil uil-cog", scale: "small", view: "flat" }), h("div", { slot: "content" }, h("dvn-menu", { leaned: true, width: "200px" }, h("dvn-menu-item", { onClick: () => this.showEditProductForm = true }, "Settings"), ['damga', 'sepet'].includes(this.product.type) &&
        h("stencil-route-link", { url: `/products/${this.product._id}/api-keys` }, h("dvn-menu-item", null, "API Keys")))))))));
    };
    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}`;
    return (h(Host, null, this.renderHeader(), this.renderEditProductForm(), this.renderDeleteProductConfirmation(), h(Tag, { apiData: apiData, apiKimlik: apiKimlik, apiDevingen: apiDevingen, apiHeaders: {
        Authorization: apiKimlik.api.getDefaults().headers['Authorization'],
        'devingen-product-id': this.product._id,
      }, product: product, products: products, workspaceOwnerships: workspaceOwnerships })));
  }
};

export { DevingenProductPage as devingen_product_page };
