import { r as registerInstance, h, H as Host } from './index-ecdfb761.js';
import { p as prod } from './services-63217112.js';
import { S as SingleChoiceField } from './index-23baeeef.js';
import { D as DvnDataAdapter } from './DvnDataAdapter-536a3258.js';
import './FieldGenerator-3ae403b2.js';
import './BaseField-0b01dc2c.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) {
    registerInstance(this, hostRef);
    this.renderSettingsForm = () => {
      if (!this.showSettingsForm) {
        return null;
      }
      return (h("dvn-dialog", { shown: true, onDismiss: () => this.showSettingsForm = false, clip: false }, h("doldurma-form", { fields: [
          new SingleChoiceField('databaseProduct', 'Database', [], '', 'select')
            .setDataProvider(new DevingenDataProviderProducts(this.apiDevingen, this.product.workspace._id, 'taban'))
            .setDataAdapter(new 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 (h(Host, null, h("div", { class: "d-flex justify-content-center" }, h("devingen-error-card", { error: error, message: "Unexpected error occurred." }))));
    }
    if (!databaseProductSettings) {
      return (h(Host, { class: "d-flex justify-content-center" }, h("dvn-progress", { indefinite: true }), this.renderSettingsForm()));
    }
    return (h(Host, { class: "d-flex flex-grow-1 flex-column" }, h("kimlik-home", { apiEndpoint: `${prod.kimlik}/${databaseProductSettings.databaseName}`, apiHeaders: this.apiHeaders }), this.renderSettingsForm()));
  }
};

export { DevingenProductRootKimlik as devingen_product_root_kimlik };
