import { DvnDataAdapter } from '@dvn/core/adapters/DvnDataAdapter';
import { DataTransferField } from '@dvn/core/doldurma/src/fields/query-include/data-transfer-field';
import { SingleChoiceField } from '@dvn/core/doldurma/src/fields/single-choice';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { Component, h, Host, Prop, State } from '@stencil/core';
import { DvnDataProviderLoadBalancers } from '../../util/DvnDataProviderLoadBalancers';
import { convertDomainToFormValues, convertFormValuesToDomain } from './util';
export class DamgaDomainForm {
  constructor() {
    this.onSubmitForm = (event) => {
      const { values } = event.detail;
      const body = convertFormValuesToDomain(values);
      const promise = values._id
        ? this.apiDamga.updateDomain(values._id, body)
        : this.apiDamga.createDomain(body);
      this.isPending = true;
      promise.then(this.onChange)
        .catch(error => {
        var _a;
        this.formErrors = [(_a = error.data) === null || _a === void 0 ? void 0 : _a.error];
      })
        .finally(() => this.isPending = false);
    };
  }
  render() {
    const { formErrors, isPending, item } = this;
    const values = convertDomainToFormValues(item);
    return (h(Host, null,
      h("doldurma-form", { fields: [
          new DataTransferField('_id')
            .setValue(item === null || item === void 0 ? void 0 : item._id),
          new TextField('domain', 'Domain')
            .setValue(values.domain)
            .require(),
          new SingleChoiceField('loadBalancer', 'Load Balancer', [], '', 'select')
            .setDataProvider(new DvnDataProviderLoadBalancers(this.apiDamga))
            .setDataAdapter(new DvnDataAdapter('name', '_id'))
            .setValue(values.loadBalancer)
            .require(),
        ], onSubmit: this.onSubmitForm, submitButtonLabel: item ? 'Update' : 'Create', progressing: isPending, formErrors: formErrors })));
  }
  static get is() { return "damga-domain-form"; }
  static get properties() { return {
    "apiDamga": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DamgaApi",
        "resolved": "DamgaApi",
        "references": {
          "DamgaApi": {
            "location": "import",
            "path": "../../api/damga-api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "onChange": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "() => void",
        "resolved": "() => void",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "item": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Domain",
        "resolved": "Domain",
        "references": {
          "Domain": {
            "location": "import",
            "path": "../../models/domain"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "isPending": {},
    "formErrors": {}
  }; }
}
