// import { SubForm } from './SubForm';
import { Component, Event, h, Host, Prop, State } from '@stencil/core';
export class DvnFieldInputSubForm {
  constructor() {
    this.getParentValue = () => (this.value || new Array(this.field.minCount).fill(undefined));
  }
  componentWillLoad() {
    if (this.field.maxCount === 1) {
      return;
    }
    this.subFormKeys = this.getParentValue().map(() => `${Math.random()}`);
  }
  render() {
    const { field, errors, subFormKeys, value } = this;
    const hasError = errors && errors.length > 0;
    const error = hasError ? errors[0] : undefined;
    if (field.maxCount === 1) {
      // add values to the fields
      const fieldsWithValues = field.fields.map(f => (Object.assign(Object.assign({}, f), { value: (value || {})[f.id] })));
      return (h(Host, null,
        h("div", { class: {
            'form-container': true,
            'separated': field.separated,
          } },
          h("doldurma-form", { fields: fieldsWithValues, layout: field.layout, onChange: (event) => {
              event.stopImmediatePropagation();
              this.change.emit(event.detail);
            } })),
        h("div", null, error)));
    }
    const parentValue = this.getParentValue();
    return (h(Host, null,
      parentValue.map((currentChildValue, i) => {
        const fieldsWithValues = currentChildValue
          ? field.fields.map(f => (Object.assign(Object.assign({}, f), { value: currentChildValue[f.id] })))
          : field.fields;
        return (h("div", { key: subFormKeys[i], class: {
            'form-container': true,
            'separated': field.separated,
          } },
          h("doldurma-form", { fields: fieldsWithValues, layout: field.layout, onChange: (event) => {
              event.stopImmediatePropagation();
              const newParentValue = [...parentValue];
              newParentValue[i] = event.detail;
              this.change.emit(newParentValue);
            } }),
          parentValue.length > field.minCount &&
            h("dvn-button", { class: "remove-input-button", shape: "circle", scale: "x-small", tint: "danger", 
              // view="ghost"
              leaned: true, onClick: (e) => {
                e.stopImmediatePropagation();
                this.subFormKeys = subFormKeys.filter((_, j) => j !== i);
                this.change.emit(parentValue.filter((_, j) => j !== i));
              } },
              h("i", { class: "uil uil-minus" }))));
      }),
      parentValue.length < field.maxCount &&
        h("dvn-button", { class: "add-input-button", shape: "circle", view: "flat", scale: "small", leaned: true, onClick: (e) => {
            e.stopImmediatePropagation();
            this.subFormKeys = [...subFormKeys, `${Math.random()}`];
            this.change.emit([...parentValue, undefined]);
          } },
          h("i", { class: "uil uil-plus" })),
      h("div", null, error)));
  }
  static get is() { return "dvn-field-input-sub-form"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-field-input-sub-form.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-field-input-sub-form.css"]
  }; }
  static get properties() { return {
    "disabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "disabled",
      "reflect": false
    },
    "field": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "SubFormField",
        "resolved": "SubFormField",
        "references": {
          "SubFormField": {
            "location": "import",
            "path": "../sub-form"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "errors": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "any[]",
        "resolved": "any[]",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "value": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "value",
      "reflect": false
    }
  }; }
  static get states() { return {
    "subFormKeys": {}
  }; }
  static get events() { return [{
      "method": "change",
      "name": "change",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
}
