import { BooleanField } from '@dvn/core/doldurma/src/fields/boolean';
import { DataTransferField } from '@dvn/core/doldurma/src/fields/query-include/data-transfer-field';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ModifierTrimmer } from '@dvn/core/doldurma/src/interceptors/modifiers/ModifierTrimmer';
import { ValidatorRegex } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorRegex';
import { Component, h, Host, Prop, State } from '@stencil/core';
const domainRegex = '(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]';
export class DamgaTargetGroupForm {
  constructor() {
    this.onSubmitForm = (event) => {
      const { values } = event.detail;
      const body = {
        type: 'url',
        targets: [{ value: values.target }],
        overrideHost: !!values.overrideHost,
        useTLS: !!values.useTLS,
        name: values.name,
        prefix: values.prefix,
      };
      const promise = values._id
        ? this.apiDamga.updateTargetGroup(values._id, body)
        : this.apiDamga.createTargetGroup(body);
      this.isPending = true;
      promise.then(this.onChange)
        .catch(error => {
        var _a;
        this.formErrors = (_a = error.data) === null || _a === void 0 ? void 0 : _a.errors;
      })
        .finally(() => this.isPending = false);
    };
  }
  render() {
    const { formErrors, isPending, item } = this;
    return (h(Host, null,
      h("doldurma-form", { fields: [
          new DataTransferField('_id')
            .setValue(item === null || item === void 0 ? void 0 : item._id),
          new TextField('name', 'Name')
            .setValue(item === null || item === void 0 ? void 0 : item.name)
            .require(),
          new TextField('target', 'Target', 'acme.co, acme.co:5678, 1.2.3.4:5678', 'Traffic will be forwarded to this address. Should not contain protocol prefix.').addInterceptor('onBlur', new ValidatorRegex(domainRegex, 'Not a valid domain or address.'))
            .addInterceptor('onSubmit', new ModifierTrimmer())
            .setValue(item === null || item === void 0 ? void 0 : item.targets[0].value)
            .require(),
          new BooleanField('overrideHost', 'Override Host', 'Overrides the target host with domain while accessing the target.').setValue(item === null || item === void 0 ? void 0 : item.overrideHost),
          new BooleanField('useTLS', 'Use TLS', 'Uses **https** instead of **http** while accessing the target.').setValue(item === null || item === void 0 ? void 0 : item.useTLS),
          new TextField('prefix', 'Prefix')
            .setValue(item === null || item === void 0 ? void 0 : item.prefix),
        ], onSubmit: this.onSubmitForm, submitButtonLabel: item ? 'Update' : 'Create', progressing: isPending, formErrors: formErrors })));
  }
  static get is() { return "damga-target-group-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": "TargetGroup",
        "resolved": "TargetGroup",
        "references": {
          "TargetGroup": {
            "location": "import",
            "path": "../../models/target-group"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "isPending": {},
    "formErrors": {}
  }; }
}
