import { openBlock, createElementBlock, renderSlot } from 'vue';
import { ObjectUtils, DomHandler, DataHandler, RowStatus } from 'primevue/utils';

var script = {
  name: 'Form',
  emits: ['row-new-init', 'item-change'],
  props: {
    disabled: {
      type: Boolean,
      "default": false
    },
    readonly: {
      type: Boolean,
      "default": false
    },
    labelWidth: {
      type: [String, Number],
      "default": ''
    },
    hideRequiredAsterisk: {
      type: Boolean,
      "default": false
    }
  },
  provide: function provide() {
    return {
      $parentInstance: this
    };
  },
  data: function data() {
    return {
      d_primaryData: null,
      d_modified: false
    };
  },
  methods: {
    getChildren: function getChildren() {
      return this.$slots["default"] ? this.$slots["default"]() : null;
    },
    recursiveGetChildren: function recursiveGetChildren(children, results) {
      var _this = this;
      if (!results) {
        results = [];
      }
      if (children && children.length) {
        children.forEach(function (child) {
          if (child.type.name == 'FormItem') {
            results.push(child);
          } else if (child.children && child.children instanceof Array) {
            _this.recursiveGetChildren(child.children, results);
          } else if (child.children && typeof child.children["default"] === 'function') {
            _this.recursiveGetChildren(child.children["default"](), results);
          }
        });
      }
      return results;
    },
    formItemProp: function formItemProp(item, prop) {
      return ObjectUtils.getVNodePropBase(item, prop);
    },
    setFocus: function setFocus(field) {
      var _this2 = this;
      if (!field) return;
      setTimeout(function () {
        var itemEl = _this2.$el.querySelector("[data-field=".concat(field));
        if (itemEl) {
          // itemEl.scrollIntoView && itemEl.scrollIntoView({ block: 'nearest', inline: 'start' });
          var focusableEl = DomHandler.getFirstFocusableElement(itemEl);
          focusableEl && focusableEl.focus();
        }
      }, 50);
    },
    setData: function setData(newData) {
      this.d_primaryData = newData ? DataHandler.Proxy(newData, 1, this._interalSetModified) : null;
      this.d_modified = false;
    },
    getData: function getData() {
      return this.d_primaryData;
    },
    "new": function _new() {
      var _items = this.items || [];
      var rowData = this.d_primaryData ? DataHandler.Initialize(this.d_primaryData, 1, this._interalSetModified) : {};
      for (var i = 0; i < _items.length; i++) {
        var item = _items[i];
        var field = this.formItemProp(item, 'field') || '';
        var initial = this.formItemProp(item, 'initial');
        var colType = this.formItemProp(item, 'colType') || '';
        if (field) {
          rowData[field] = initial !== null && initial !== undefined && initial !== '' ? ObjectUtils.initailValue(initial, colType) : null;
        }
      }
      if (!('rowId' in rowData) || !('originalData' in rowData)) {
        rowData = DataHandler.Proxy(rowData, 1, this._interalSetModified);
      }
      DomHandler.clearSelection();
      this.$emit('row-new-init', rowData);
      rowData['rowStatus'] = RowStatus.New;
      this.d_primaryData = rowData;
      this.d_modified = false;
      return rowData; // 返回新增行的数据，以便对新增行的数据进行赋值 
    },
    setItem: function setItem(fieldName, value) {
      this.d_primaryData[fieldName] = value;
    },
    getItem: function getItem(fieldName) {
      return this.d_primaryData[fieldName];
    },
    reset: function reset() {
      this.d_primaryData = null;
      this.d_modified = false;
    },
    resetUpdate: function resetUpdate() {
      if (!this.d_primaryData) return;
      this.d_primaryData.resetUpdate();
      this.d_modified = false;
    },
    isModified: function isModified() {
      /*
      if(!this.d_primaryData)
          return false ;
       return (this.d_primaryData.rowStatus=== RowStatus.DataModified ||  this.d_primaryData.rowStatus=== RowStatus.NewModified) ? true:false;
      */

      return this.d_modified;
    },
    onKeydown: function onKeydown(event) {
      var target = event.target || event.srcElement;
      if ((target.nodeName || '').toUpperCase() === 'TEXTAREA') return;
      switch (event.code) {
        case 'Enter':
        case 'NumpadEnter':
          {
            var focusableEl = DomHandler.getNextFocusableElement(this.$el, target);
            if (focusableEl) {
              event.preventDefault();
              event.stopPropagation();
              focusableEl.focus();
            }
          }
          break;
      }
    },
    onItemChange: function onItemChange(event) {
      this.$emit('item-change', event);
    },
    _interalSetModified: function _interalSetModified(newData) {
      this.d_modified = newData;
    }
  },
  computed: {
    primaryData: function primaryData() {
      return this.d_primaryData;
    },
    items: function items() {
      var children = this.getChildren();
      if (!children) {
        return;
      }
      return this.recursiveGetChildren(children, []);
    }
  }
};

function render(_ctx, _cache, $props, $setup, $data, $options) {
  return openBlock(), createElementBlock("div", {
    "class": "form-container",
    onKeydown: _cache[0] || (_cache[0] = function () {
      return $options.onKeydown && $options.onKeydown.apply($options, arguments);
    })
  }, [renderSlot(_ctx.$slots, "default")], 32 /* NEED_HYDRATION */);
}

script.render = render;
script.__file = "components/lib/form/Form.vue";

export { script as default };
