'use strict';

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

const index = require('./index-1141a245.js');
const index$1 = require('./index-61f9621a.js');
const ModifierTrimmer = require('./ModifierTrimmer-1463d100.js');
const ValidatorLength = require('./ValidatorLength-06e5c6f8.js');
require('./FieldGenerator-279a646b.js');
require('./BaseField-381327a9.js');
require('./InterceptorHandler-09a1e84b.js');

const DevingenProductPage = class {
  constructor(hostRef) {
    index.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 (index.h("div", { class: "container" }, index.h("div", { class: "row" }, index.h("div", { class: "col d-flex align-items-center pt-2 pb-3" }, index.h("h2", { class: "m-0" }, (_a = this.product) === null || _a === void 0 ? void 0 : _a.name), index.h("dvn-dropdown", { placement: "rightTop", trigger: "click", leaned: true }, index.h("dvn-icon-button", { iconName: "uil uil-cog", scale: "small", view: "flat" }), index.h("div", { slot: "content" }, index.h("dvn-menu", { leaned: true, width: "200px" }, index.h("dvn-menu-item", { onClick: () => this.showEditProductForm = true }, "Settings"), ['damga', 'sepet'].includes(this.product.type) &&
        index.h("stencil-route-link", { url: `/products/${this.product._id}/api-keys` }, index.h("dvn-menu-item", null, "API Keys")))))))));
    };
    this.renderEditProductForm = () => {
      if (!this.showEditProductForm) {
        return null;
      }
      return (index.h("dvn-dialog", { shown: true, onDismiss: () => this.showEditProductForm = false }, index.h("doldurma-form", { fields: [
          new index$1.TextField('name', 'Name').addInterceptor('onSubmit', new ValidatorLength.ValidatorLength(2, 32))
            .addInterceptor('onSubmit', new ModifierTrimmer.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 (index.h("dvn-dialog", { shown: true, onDismiss: () => {
          this.showDeleteConfirmation = false;
          this.deleteError = undefined;
        }, title: "Deletion" }, index.h("p", null, "Are you sure to delete this product? This action cannot be undone."), this.deleteError &&
        index.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.'), index.h("div", { slot: "actions" }, index.h("dvn-button", { view: "flat", onClick: () => {
          this.showDeleteConfirmation = false;
          this.deleteError = undefined;
        } }, "Cancel"), index.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 (index.h("div", { class: "d-flex justify-content-center pt-3" }, index.h("dvn-progress", { indefinite: true })));
    }
    let Tag = `devingen-product-root-${product.type}`;
    return (index.h(index.Host, null, this.renderHeader(), this.renderEditProductForm(), this.renderDeleteProductConfirmation(), index.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 })));
  }
};

exports.devingen_product_page = DevingenProductPage;
