import { Component, h, Host, Prop, State } from '@stencil/core';
import services from "../../configs/services";
import { SingleChoiceField } from "@dvn/core/doldurma/src/fields/single-choice";
import { DevingenDataProviderProducts } from "../../util/DevingenDataProviderProducts";
import { DvnDataAdapter } from "@dvn/core/adapters/DvnDataAdapter";
export class DevingenProductRootKimlik {
  constructor() {
    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) {
        // update
      }
      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: `${services.kimlik}/${databaseProductSettings.databaseName}`, apiHeaders: this.apiHeaders }),
      this.renderSettingsForm()));
  }
  static get is() { return "devingen-product-root-kimlik"; }
  static get properties() { return {
    "apiHeaders": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "api-headers",
      "reflect": false
    },
    "apiDevingen": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DevingenApi",
        "resolved": "DevingenApi",
        "references": {
          "DevingenApi": {
            "location": "import",
            "path": "../../api/devingen-api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "product": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Product",
        "resolved": "Product",
        "references": {
          "Product": {
            "location": "import",
            "path": "../../models/product"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "productSettings": {},
    "databaseProductSettings": {},
    "showSettingsForm": {}
  }; }
}
