import { r as registerInstance, c as createEvent, h, g as getElement } from './index-c948674f.js';

const Form = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.onSubmit = createEvent(this, "submit", 7);
    this.value = {};
  }
  onButtonSubmit(e) {
    e.stopPropagation();
    this.value = this.getFormValue();
    this.onSubmit.emit({
      value: this.value
    });
  }
  onButtonReset(e) {
    e.stopPropagation();
    this.form.reset();
  }
  componentDidLoad() {
    this.value = this.getFormValue();
    Object.defineProperty(this.el, 'value', {
      get: () => this.value,
      configurable: true
    });
  }
  componentDidRender() {
    if (!this.originalAppendChild) {
      this.originalAppendChild = this.el.appendChild;
      this.originalInsertBefore = this.el.insertBefore;
      this.originalReplaceChild = this.el.replaceChild;
      this.originalRemoveChild = this.el.removeChild;
    }
    if (!this.form) {
      this.el.appendChild = this.originalAppendChild;
      this.el.insertBefore = this.originalInsertBefore;
      this.el.replaceChild = this.originalReplaceChild;
      this.el.removeChild = this.originalRemoveChild;
      return;
    }
    this.el.appendChild = (newChild) => {
      return this.form.appendChild(newChild);
    };
    this.el.insertBefore = (newChild, refChild) => {
      return this.form.insertBefore(newChild, refChild);
    };
    this.el.replaceChild = (newChild, oldChild) => {
      return this.form.replaceChild(newChild, oldChild);
    };
    this.el.removeChild = (oldChild) => {
      return this.form.removeChild(oldChild);
    };
  }
  getFormValue() {
    const el = this.el;
    const elements = [];
    const tagElements = el.getElementsByTagName('input');
    for (let j = 0; j < tagElements.length; j++) {
      elements.push(tagElements[j]);
    }
    const formItem = {};
    const hash = {};
    elements.forEach(item => {
      if (typeof item.name !== 'string')
        return;
      if (item.className.indexOf('weui-switch') !== -1) {
        formItem[item.name] = item.checked;
        return;
      }
      if (item.type === 'radio') {
        if (item.checked) {
          hash[item.name] = true;
          formItem[item.name] = item.value;
        }
        else {
          if (!hash[item.name]) {
            formItem[item.name] = '';
          }
        }
        return;
      }
      if (item.type === 'checkbox') {
        if (item.checked) {
          if (hash[item.name]) {
            formItem[item.name].push(item.value);
          }
          else {
            hash[item.name] = true;
            formItem[item.name] = [item.value];
          }
        }
        else {
          if (!hash[item.name]) {
            formItem[item.name] = [];
          }
        }
        return;
      }
      formItem[item.name] = item.value;
    });
    const textareaElements = el.getElementsByTagName('textarea');
    const textareaEleArr = [];
    for (let i = 0; i < textareaElements.length; i++) {
      textareaEleArr.push(textareaElements[i]);
    }
    textareaEleArr.forEach(v => {
      if (typeof v.name !== 'string')
        return;
      formItem[v.name] = v.value;
    });
    return formItem;
  }
  render() {
    return (h("form", { ref: dom => {
        this.form = dom;
      } }, h("slot", null)));
  }
  get el() { return getElement(this); }
};

export { Form as taro_form_core };

//# sourceMappingURL=taro-form-core.entry.js.map