import { openBlock, createElementBlock, normalizeStyle, normalizeClass, renderSlot, toDisplayString, createCommentVNode, createBlock, resolveDynamicComponent, mergeProps } from 'vue';
import { RowStatus } from 'primevue/utils';

var script = {
  name: 'FormItem',
  inject: {
    $parentInstance: {
      "default": null
    }
  },
  props: {
    editor: {
      type: String,
      "default": null
    },
    field: {
      type: String,
      "default": null
    },
    colType: {
      type: String,
      "default": null
    },
    initial: {
      type: null,
      "default": null
    },
    label: {
      type: String,
      "default": ''
    },
    labelWidth: {
      type: [String, Number],
      "default": null
    },
    suffix: {
      type: String,
      "default": ''
    },
    required: {
      type: Boolean,
      "default": false
    },
    disabled: {
      type: [Boolean, String, Function],
      "default": null
    },
    readonly: {
      type: [Boolean, String, Function],
      "default": null
    },
    error: {
      type: String,
      "default": ''
    },
    align: {
      type: String,
      "default": null
    },
    className: {
      type: String,
      "default": null
    },
    style: {
      type: null,
      "default": null
    },
    cols: {
      type: [Number, String],
      "default": 0
    },
    offset: {
      type: [Number, String],
      "default": 0
    }
  },
  data: function data() {
    return {};
  },
  methods: {
    onUpdateModelValue: function onUpdateModelValue(value) {
      this.$parentInstance && this.$parentInstance.onItemChange({
        rowData: this.$parentInstance.primaryData,
        field: this.field,
        value: value
      });
    }
  },
  computed: {
    primaryData: function primaryData() {
      var data = this.$parentInstance && this.$parentInstance.primaryData;
      return data || {};
    },
    containerClass: function containerClass() {
      var colClass = this.cols > 0 ? 'col-' + this.cols : null;
      var offsetClass = this.offset > 0 ? 'col-offset-' + this.offset : null;
      return [colClass, offsetClass, 'form-item'];
    },
    containerStyle: function containerStyle() {
      var style = this.style;
      if (typeof style === 'cls') {
        style = bodyStyle(this.primaryData, this.field);
      }
      return style;
    },
    showLabel: function showLabel() {
      var tagName = this.editor;
      return !this.label || tagName === 'Checkbox' || tagName === 'RadioButton' || tagName === 'TriStateCheckbox' ? false : true;
    },
    labelClass: function labelClass() {
      return ["form-item-label", {
        'required': this.required
      }];
    },
    labelWidthStyle: function labelWidthStyle() {
      var width = this.labelWidth ? this.labelWidth : this.$parentInstance ? this.$parentInstance.labelWidth : null;
      return width ? ["width: ".concat(width)] : null;
    },
    isDisabled: function isDisabled() {
      if (!this.$parentInstance || !this.$parentInstance.primaryData) return false;
      if (this.$parentInstance.disabled) return true;
      var data = this.primaryData;
      if (this.disabled === null || this.disabled === undefined || this.disabled === '') return false;else if (typeof this.disabled === 'boolean') return this.disabled;else if (typeof this.disabled === 'function') return this.disabled(data, this.field);else if (this.disabled === 'notnew') {
        var status = data['rowStatus'];
        return status === RowStatus.New || status === RowStatus.NewModified ? false : true;
      }
      return this.disabled === '1' ? true : false;
    },
    isReadonly: function isReadonly() {
      if (!this.$parentInstance || !this.$parentInstance.primaryData || this.$parentInstance.readonly) return true;
      var data = this.primaryData;
      if (this.readonly === null || this.readonly === undefined || this.readonly === '') return false;else if (typeof this.readonly === 'boolean') return this.readonly;else if (typeof this.readonly === 'function') return this.readonly(data, this.field);else if (this.readonly === 'notnew') {
        var status = data['rowStatus'];
        return status === RowStatus.New || status === RowStatus.NewModified ? false : true;
      }
      return this.readonly === '1' ? true : false;
    }
  }
};

var _hoisted_1 = ["data-field"];
var _hoisted_2 = {
  key: 0,
  "class": "form-item-suffix"
};
function render(_ctx, _cache, $props, $setup, $data, $options) {
  return openBlock(), createElementBlock("div", {
    style: normalizeStyle($options.containerStyle),
    "class": normalizeClass($options.containerClass),
    "data-field": $props.field
  }, [renderSlot(_ctx.$slots, "label", {}, function () {
    return [$options.showLabel ? (openBlock(), createElementBlock("label", {
      key: 0,
      "class": normalizeClass($options.labelClass),
      style: normalizeStyle($options.labelWidthStyle)
    }, toDisplayString($props.label), 7 /* TEXT, CLASS, STYLE */)) : createCommentVNode("v-if", true)];
  }), renderSlot(_ctx.$slots, "default", {
    primaryData: $options.primaryData,
    field: $props.field,
    disabled: $options.isDisabled,
    readonly: $options.isReadonly,
    "onUpdate:modelValue": _cache[0] || (_cache[0] = function () {
      return $options.onUpdateModelValue && $options.onUpdateModelValue.apply($options, arguments);
    })
  }, function () {
    return [$props.field && $props.editor ? (openBlock(), createBlock(resolveDynamicComponent($props.editor), mergeProps({
      key: 0,
      modelValue: $options.primaryData[$props.field],
      "onUpdate:modelValue": _cache[1] || (_cache[1] = function ($event) {
        return $options.primaryData[$props.field] = $event;
      }),
      "class": [$props.className, {
        'form-item-content': $props.editor !== 'InputSwitch'
      }]
    }, _ctx.$attrs, {
      align: $props.align,
      label: $props.label,
      disabled: $options.isDisabled,
      readonly: $options.isReadonly,
      "onUpdate:modelValue": $options.onUpdateModelValue
    }), null, 16 /* FULL_PROPS */, ["modelValue", "class", "align", "label", "disabled", "readonly", "onUpdate:modelValue"])) : createCommentVNode("v-if", true)];
  }), renderSlot(_ctx.$slots, "suffix", {}, function () {
    return [$props.suffix ? (openBlock(), createElementBlock("span", _hoisted_2, toDisplayString($props.suffix), 1 /* TEXT */)) : createCommentVNode("v-if", true)];
  })], 14 /* CLASS, STYLE, PROPS */, _hoisted_1);
}

script.render = render;
script.__file = "components/lib/formitem/FormItem.vue";

export { script as default };
