'use strict';

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

const index = require('./index-1141a245.js');
const services = require('./services-9cb6ea3a.js');
const index$1 = require('./index-9d3d3d15.js');
const DvnDataAdapter = require('./DvnDataAdapter-8f89f436.js');
require('./FieldGenerator-279a646b.js');
require('./BaseField-381327a9.js');

class DevingenDataProviderProducts {
  constructor(devingenApi, workspaceId, productType = '') {
    this.devingenApi = devingenApi;
    this.workspaceId = workspaceId;
    this.productType = productType;
  }
  getInitialData() {
    return this.getData();
  }
  getData() {
    const { productType, workspaceId } = this;
    return new Promise((resolve) => {
      this.devingenApi.getProducts({ productType, workspaceId })
        .then(response => resolve(response.results.sort((a, b) => a.name.localeCompare(b.name))));
    });
  }
  getQuickSearchText() {
    return '';
  }
  isQuickSearchEnabled() {
    return false;
  }
  setInitialData(_data) {
  }
  setQuickSearchText(_text) {
  }
}

const DevingenProductRootKimlik = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.renderSettingsForm = () => {
      if (!this.showSettingsForm) {
        return null;
      }
      return (index.h("dvn-dialog", { shown: true, onDismiss: () => this.showSettingsForm = false, clip: false }, index.h("doldurma-form", { fields: [
          new index$1.SingleChoiceField('databaseProduct', 'Database', [], '', 'select')
            .setDataProvider(new DevingenDataProviderProducts(this.apiDevingen, this.product.workspace._id, 'taban'))
            .setDataAdapter(new DvnDataAdapter.DvnDataAdapter('name', '_id'))
            .require(),
        ], onSubmit: this.onSubmitEditSettingsForm, submitButtonLabel: "Save" })));
    };
    this.onSubmitEditSettingsForm = (event) => {
      const { values } = event.detail;
      if (this.productSettings) ;
      else {
        // create
        this.apiDevingen.createProductSettingsKimlik(this.product._id, values.databaseProduct, '', {})
          .then(() => {
          this.fetchProductSettings();
          this.showSettingsForm = false;
        })
          .catch(error => this.error = error);
      }
    };
  }
  componentWillLoad() {
    this.fetchProductSettings();
  }
  fetchProductSettings() {
    this.apiDevingen.findProductSettingsKimlik(this.product._id)
      .then(response => {
      this.productSettings = response;
      this.fetchDatabaseProductSettings();
    })
      .catch((e) => {
      var _a;
      if (((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.error) === 'product-setting-not-found-for-product') {
        this.showSettingsForm = true;
      }
      else {
        this.error = e;
      }
    });
  }
  fetchDatabaseProductSettings() {
    this.apiDevingen.findProductSettingsTaban(this.productSettings.databaseProduct._id)
      .then(response => this.databaseProductSettings = response)
      .catch((e) => {
      console.log(e);
      this.error = e;
    });
  }
  render() {
    const { error, databaseProductSettings } = this;
    if (error) {
      return (index.h(index.Host, null, index.h("div", { class: "d-flex justify-content-center" }, index.h("devingen-error-card", { error: error, message: "Unexpected error occurred." }))));
    }
    if (!databaseProductSettings) {
      return (index.h(index.Host, { class: "d-flex justify-content-center" }, index.h("dvn-progress", { indefinite: true }), this.renderSettingsForm()));
    }
    return (index.h(index.Host, { class: "d-flex flex-grow-1 flex-column" }, index.h("kimlik-home", { apiEndpoint: `${services.prod.kimlik}/${databaseProductSettings.databaseName}`, apiHeaders: this.apiHeaders }), this.renderSettingsForm()));
  }
};

exports.devingen_product_root_kimlik = DevingenProductRootKimlik;
