import { DataTransferField } from '@dvn/core/doldurma/src/fields/query-include/data-transfer-field';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { Component, h, Host, Prop, State } from '@stencil/core';
import { convertBucketToFormValues, convertFormValuesToBucket } from './util';
import { SubFormField } from '@dvn/core/doldurma/src/fields/sub-form';
import { SingleChoiceField } from '@dvn/core/doldurma/src/fields/single-choice';
import { ConcealerConditional, Equal } from '@dvn/core/doldurma/src/interceptors/concealers/ConcealerGenericRule';
export class SepetBucketForm {
  constructor() {
    this.onSubmitForm = (event) => {
      const { values } = event.detail;
      const body = convertFormValuesToBucket(values);
      const promise = values._id
        ? this.apiSepet.updateBucket(values._id, body)
        : this.apiSepet.createBucket(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 = convertBucketToFormValues(item);
    return (h(Host, null,
      h("doldurma-form", { fields: [
          new DataTransferField('_id')
            .setValue(item === null || item === void 0 ? void 0 : item._id),
          new SingleChoiceField('type', 'Bucket type', [
            { label: 'Simple File Storage', value: 'simple-folder' },
            { label: 'Static Website Host', value: 'website-host' },
          ], '', 'select', '')
            .setDisabled(!!(item === null || item === void 0 ? void 0 : item._id))
            .setValue(values.versionIdentifier === 'header' ? 'website-host' : 'simple-folder')
            .require(),
          new TextField('domain', 'Domain', '', 'Used in the URL like \'acme\' for \'acme.sepet.devingen.io\'')
            .setValue(values.domain)
            .setDisabled(!!(item === null || item === void 0 ? void 0 : item._id))
            .require(),
          new TextField('indexPagePath', 'Index page path', '', 'Used as SPAs\' index page when the root URL is called.')
            .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'website-host')))
            .setVisibility(false)
            .setValue(values.indexPagePath)
            .require(),
          new TextField('errorPagePath', 'Error page path', '', 'Used as SPAs\' error page when a file is not found.')
            .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'website-host')))
            .setVisibility(false)
            .setValue(values.errorPagePath)
            .require(),
          // new BooleanField('isCacheEnabled', 'Is cache enabled?', '', 'Cache the file in CDN for subsequent requests.')
          //   .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'website-host')))
          //   .setVisibility(false)
          //   .setValue(values.isCacheEnabled),
          new SubFormField('responseHeaders', 'Response Headers', [
            new TextField('name', '', 'Header Name (e.g. Authorization, Content-Type)')
              .require(),
            new TextField('value', '', 'Header Value')
              .require(),
          ], 'HTTP response headers returned by CDN to get file responses. Useful for browser cache management.')
            .setMaxCount(10)
            .addInterceptor('onFormChange', new ConcealerConditional(new Equal('type', 'website-host')))
            .setVisibility(false)
            .setValue(values.responseHeaders)
        ], onSubmit: this.onSubmitForm, submitButtonLabel: item ? 'Update' : 'Create', progressing: isPending, formErrors: formErrors })));
  }
  static get is() { return "sepet-bucket-form"; }
  static get properties() { return {
    "apiSepet": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "SepetApi",
        "resolved": "SepetApi",
        "references": {
          "SepetApi": {
            "location": "import",
            "path": "../../api/sepet-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": "Bucket",
        "resolved": "Bucket",
        "references": {
          "Bucket": {
            "location": "import",
            "path": "../../models/bucket"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "isPending": {},
    "formErrors": {}
  }; }
}
