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 { SubFormField } from '@dvn/core/doldurma/src/fields/sub-form';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ConcealerConditional, Equal } from '@dvn/core/doldurma/src/interceptors/concealers/ConcealerGenericRule';
import { Component, h, Host, Prop, State } from '@stencil/core';
import { DvnDataProviderTargetGroups } from '../../util/DvnDataProviderTargetGroups';
import { convertFormValuesToLoadBalancer, convertLoadBalancerToFormValues } from './util';
export class DamgaLoadBalancerForm {
  constructor() {
    this.onSubmitForm = (event) => {
      const { values } = event.detail;
      const body = convertFormValuesToLoadBalancer(values);
      const promise = values._id
        ? this.apiDamga.updateLoadBalancer(values._id, body)
        : this.apiDamga.createLoadBalancer(body);
      this.isPending = true;
      promise.then(this.onChange)
        .finally(() => this.isPending = false);
    };
  }
  componentWillLoad() {
    this.dataProviderTargetGroups = new DvnDataProviderTargetGroups(this.apiDamga);
  }
  render() {
    const { isPending, item } = this;
    const values = convertLoadBalancerToFormValues(item);
    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(values.name)
            .require(),
          new SubFormField('listeners', 'Listeners', [
            new SingleChoiceField('targetGroup', 'Target Group', [], '', 'select')
              .setDataProvider(this.dataProviderTargetGroups)
              .setDataAdapter(new DvnDataAdapter('name', '_id'))
              .require(),
            new SubFormField('rules', 'Rules', [
              new SingleChoiceField('type', '', [
                { label: 'Cookie', value: 'cookie' },
                { label: 'Header', value: 'header' },
                { label: 'Host', value: 'host' },
                { label: 'Path', value: 'path' },
                // the ones below are not yet supported by the damga-proxy server
                // { label: 'Method', value: 'method' },
                // { label: 'Query Param', value: 'query' },
                // { label: 'Source IP', value: 'source-ip' },
              ], '', 'select', 'Type')
                .require(),
              new SubFormField('hostValue', '', [
                new TextField('value', '', 'Host value (e.g. devingen.io, khanguru.com)')
                  .require(),
              ]).setMaxCount(5)
                .setSeparated(false)
                .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'host')))
                .setVisibility(false),
              new SubFormField('pathValue', '', [
                new TextField('value', '', 'Path value (e.g. /, /en*, /api*)')
                  .require(),
              ]).setMaxCount(5)
                .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'path')))
                .setVisibility(false),
              new SubFormField('headerValue', '', [
                new TextField('name', '', 'Header Name (e.g. Authorization, Content-Type)')
                  .require(),
                new SubFormField('values', '', [
                  new TextField('value', '', 'Header Value')
                    .require(),
                ]).setMaxCount(5)
              ]).setMaxCount(5)
                .setVisibility(false)
                .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'header'))),
              new SubFormField('cookieValue', '', [
                new TextField('name', '', 'Cookie Name')
                  .require(),
                new SubFormField('values', '', [
                  new TextField('value', '', 'Cookie Value')
                    .require(),
                ]).setMaxCount(5)
              ]).setMaxCount(5)
                .setVisibility(false)
                .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'cookie'))),
            ]).setMaxCount(5),
          ])
            .setMinCount(0)
            .setMaxCount(10)
            .setValue(values.listeners),
          new SingleChoiceField('defaultTargetGroup', 'Default Listener Target', [], '', 'select')
            .setDataProvider(this.dataProviderTargetGroups)
            .setDataAdapter(new DvnDataAdapter('name', '_id'))
            .setValue(values.defaultTargetGroup)
            .require(),
        ], onSubmit: this.onSubmitForm, submitButtonLabel: item ? 'Update' : 'Create', progressing: isPending })));
  }
  static get is() { return "damga-load-balancer-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": "LoadBalancer",
        "resolved": "LoadBalancer",
        "references": {
          "LoadBalancer": {
            "location": "import",
            "path": "../../models/load-balancer"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "isPending": {}
  }; }
}
