import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { Component, h, Host, Prop, State } from '@stencil/core';
import { DataTransferField } from "@dvn/core/doldurma/src/fields/query-include/data-transfer-field";
import { SingleChoiceField } from "@dvn/core/doldurma/src/fields/single-choice";
import { ConcealerConditional, Equal } from "@dvn/core/doldurma/src/interceptors/concealers/ConcealerGenericRule";
import { SubFormField } from "@dvn/core/doldurma/src/fields/sub-form";
export class KimlikSAMLConfigForm {
  constructor() {
    this.onSubmitForm = (event) => {
      const { values } = event.detail;
      console.log(values);
      const { name, metadataURL, metadataContent, assertionConsumerServiceURL, audienceURI, serviceProviderIssuer, attributeKeyMappingTemplate, metaAttributeKeyMapping: metaAttributeKeyMappingArray = [] } = values;
      const metaAttributeKeyMapping = {};
      metaAttributeKeyMappingArray.forEach(keyValuePair => {
        if (keyValuePair.key && keyValuePair.value) {
          metaAttributeKeyMapping[keyValuePair.key] = keyValuePair.value;
        }
      });
      const body = {
        name,
        metadataURL: metadataContent ? undefined : metadataURL,
        metadataContent: metadataURL ? undefined : metadataContent,
        assertionConsumerServiceURL,
        audienceURI,
        serviceProviderIssuer,
        attributeKeyMappingTemplate,
        metaAttributeKeyMapping
      };
      const promise = values._id
        ? this.apiKimlik.updateSAMLConfig(values._id, body)
        : this.apiKimlik.createSAMLConfig(body);
      this.isPending = true;
      promise.then(this.onChange)
        .finally(() => this.isPending = false);
    };
  }
  render() {
    const { 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 SingleChoiceField('metadataSource', 'Metadata source', [
            { label: 'URL', value: 'url' },
            { label: 'XML File Content', value: 'raw' },
          ], '', 'select', 'Metadata Source')
            .setValue((item === null || item === void 0 ? void 0 : item.metadataContent) ? 'raw' : 'url')
            .require(),
          new TextField('metadataURL', 'Metadata URL')
            .setInputType('url')
            .setValue(item === null || item === void 0 ? void 0 : item.metadataURL)
            .addInterceptor('onFormChange', new ConcealerConditional(new Equal('metadataSource', 'url')))
            .require(),
          new TextField('metadataContent', 'Metadata File Content (XML)')
            .setLinesMax(10)
            .setLines(10)
            .setValue(item === null || item === void 0 ? void 0 : item.metadataContent)
            .addInterceptor('onFormChange', new ConcealerConditional(new Equal('metadataSource', 'raw')))
            .require(),
          new TextField('assertionConsumerServiceURL', 'Assertion Consumer Service URL')
            .setInputType('url')
            .setValue(item === null || item === void 0 ? void 0 : item.assertionConsumerServiceURL)
            .require(),
          new TextField('audienceURI', 'Audience URI')
            .setInputType('url')
            .setValue(item === null || item === void 0 ? void 0 : item.audienceURI)
            .require(),
          new TextField('serviceProviderIssuer', 'Service Provider Issuer')
            .setInputType('url')
            .setValue(item === null || item === void 0 ? void 0 : item.serviceProviderIssuer)
            .require(),
          new SingleChoiceField('attributeKeyMappingTemplate', 'Attribute mapping template', [
            { label: 'Azure AD', value: 'azure-ad' },
          ], 'By default the user details are expected to be found as Email, FirstName and LastName in the SAML response. You may choose predefined SAML token claims for well known SAML providers.', 'select', 'None')
            .setValue(item === null || item === void 0 ? void 0 : item.attributeKeyMappingTemplate),
          new SubFormField('metaAttributeKeyMapping', 'Meta attribute key mapping', [
            new TextField('key', '', 'Key appears in user.meta').require(),
            new TextField('value', '', 'Key appears in SAML response').require(),
          ], 'User data sent to the consumer API only contains email, firstName and lastName. You can get extra token values sent by SAML provider and receive them in user.meta object by defining the user.meta field keys and their corresponding SAML token claims.', 'horizontal').setMaxCount(10)
            .setValue(Object.entries((item === null || item === void 0 ? void 0 : item.metaAttributeKeyMapping) || {}).map(([key, value]) => ({ key, value }))),
        ], onSubmit: this.onSubmitForm, submitButtonLabel: (item === null || item === void 0 ? void 0 : item._id) ? 'Update' : 'Create', progressing: isPending })));
  }
  static get is() { return "kimlik-saml-config-form"; }
  static get properties() { return {
    "apiKimlik": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "KimlikApi",
        "resolved": "KimlikApi",
        "references": {
          "KimlikApi": {
            "location": "import",
            "path": "@dvn/kimlik/api/kimlik-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": "SAMLConfig",
        "resolved": "SAMLConfig",
        "references": {
          "SAMLConfig": {
            "location": "import",
            "path": "@dvn/kimlik/api/models/saml-config"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "isPending": {},
    "createAPIKeyResponse": {}
  }; }
}
