import { h } from './index-b6f10a81.js';
import { r as registerField, T as TextField } from './index-ec7dc2ad.js';
import { B as BaseField } from './BaseField-d94c5b14.js';
import { D as DateField } from './date-b270be76.js';
import { S as SingleChoiceField } from './index-94c1d7bf.js';

class ReferenceField extends BaseField {
  constructor(id, title, titleField, imageField, otherCollection, fields, isSingle = false) {
    super(id, 'reference');
    this.title = title;
    this.titleField = titleField;
    this.imageField = imageField;
    this.otherCollection = otherCollection;
    this.fields = fields;
    this.isSingle = isSingle;
  }
  static decode(json) {
    return Object.assign(Object.create(ReferenceField.prototype), json);
  }
  showPreview() {
    this.preview = true;
    return this;
  }
  getDisplayValue(value) {
    return value;
  }
}
ReferenceField.type = 'reference';

// component generator
const render = (field, value, errors, disabled, onFieldChange, onFieldBlur) => h("dvn-field-input-date", { disabled: disabled, field: field, errors: errors, onChange: (v) => onFieldChange(field, v), onBlur: () => onFieldBlur(field), value: value });
// register 'date' field for json form generations
registerField('date', render);

class MultipleChoiceField extends BaseField {
  constructor(id, title, options, description, inputType = 'checkbox', placeholder) {
    super(id, 'multipleChoice');
    this.title = title;
    this.description = description;
    this.options = options;
    this.inputType = inputType;
    this.placeholder = placeholder;
  }
  showPreview() {
    this.preview = true;
    return this;
  }
  getOptionOfValue(value) {
    if (!this.optionsByValue) {
      this.optionsByValue = {};
      for (let i = 0; i < this.options.length; i += 1) {
        this.optionsByValue[this.options[i].value] = this.options[i];
      }
    }
    return this.optionsByValue[value] || { label: value, value };
  }
}
MultipleChoiceField.type = 'multipleChoice';

const render$1 = (field, value, errors, disabled, onFieldChange, onFieldBlur) => h("dvn-field-input-multiple-choice", { disabled: disabled, errors: errors, field: field, onChange: (e) => onFieldChange(field, e.detail), onBlur: () => onFieldBlur(field), value: value });
// register 'multipleChoice' key for json form generations
registerField('multipleChoice', render$1);

class NumberField extends BaseField {
  constructor(id, title, placeholder, description, step = 1, min = -Infinity, max = Infinity) {
    super(id, 'number');
    this.title = title;
    this.placeholder = placeholder;
    this.description = description;
    this.step = step;
    this.min = min;
    this.max = max;
  }
  showPreview() {
    this.preview = true;
    return this;
  }
  getDisplayValue(value) {
    return value;
  }
}
NumberField.type = 'number';

// component generator
const render$2 = (field, value, errors, disabled, onFieldChange, onFieldBlur) => h("dvn-field-input-number", { disabled: disabled, field: field, errors: errors, onChange: (e) => onFieldChange(field, Number(e.detail)), onBlur: () => onFieldBlur(field), value: value });
// register 'number' field for json form generations
registerField('number', render$2);

class RangeField extends BaseField {
  constructor(id, title, description, step = 1, min = 0, max = 5) {
    super(id, 'range');
    this.title = title;
    this.description = description;
    this.step = step;
    this.min = min;
    this.max = max;
  }
  showPreview() {
    this.preview = true;
    return this;
  }
  getDisplayValue(value) {
    return value;
  }
  setInputType(inputType) {
    this.inputType = inputType;
    return this;
  }
}
RangeField.type = 'range';

// component generator
const render$3 = (field, value, errors, disabled, onFieldChange, onFieldBlur) => h("dvn-field-input-range", { disabled: disabled, field: field, errors: errors, onChange: (e) => onFieldChange(field, Number(e.detail)), onBlur: () => onFieldBlur(field), value: value });
// register 'range' field for json form generations
registerField('range', render$3);

class SectionHeaderField extends BaseField {
  constructor(id, header, sectionDescription) {
    super(id, 'sectionHeader');
    this.id = id;
    this.header = header;
    this.sectionDescription = sectionDescription;
  }
}
SectionHeaderField.type = 'sectionHeader';

const render$4 = (field, 
// @ts-ignore
value, 
// @ts-ignore
errors, disabled, onFieldChange, onFieldBlur) => h("dvn-field-input-section-header", { disabled: disabled, field: field, onChange: (v) => onFieldChange(field, v), onBlur: () => onFieldBlur(field) });
// register 'sectionHeader' key for json form generations
registerField('sectionHeader', render$4);

class SubFormField extends BaseField {
  constructor(id, title, fields, description, 
  // TODO
  layout, showFieldOrder, language) {
    super(id, 'subForm');
    this.id = id;
    this.title = title;
    this.description = description;
    this.fields = fields;
    this.language = language;
    this.layout = layout;
    this.showFieldOrder = showFieldOrder;
    this.value = {};
  }
}
SubFormField.type = 'subForm';

const render$5 = (field, value, errors, disabled, onFieldChange, onFieldBlur) => h("dlm-input-sub-form", { disabled: disabled, errors: errors, field: field, onChange: (v) => onFieldChange(field, v), onBlur: () => onFieldBlur(field), value: value });
// register 'subForm' key for json form generations
registerField('subForm', render$5);

function generateFieldModelFromRawData(fieldData) {
  if (!fieldData) {
    return undefined;
  }
  switch (fieldData.type) {
    case DateField.type:
      return Object.assign(Object.create(DateField.prototype), fieldData);
    case MultipleChoiceField.type:
      return Object.assign(Object.create(MultipleChoiceField.prototype), fieldData);
    case NumberField.type:
      return Object.assign(Object.create(NumberField.prototype), fieldData);
    case RangeField.type:
      return Object.assign(Object.create(RangeField.prototype), fieldData);
    case SectionHeaderField.type:
      return Object.assign(Object.create(SectionHeaderField.prototype), fieldData);
    case SingleChoiceField.type:
      return Object.assign(Object.create(SingleChoiceField.prototype), fieldData);
    case SubFormField.type:
      return Object.assign(Object.create(SubFormField.prototype), fieldData);
    case TextField.type:
      return Object.assign(Object.create(TextField.prototype), fieldData);
    case ReferenceField.type:
      return Object.assign(Object.create(ReferenceField.prototype), fieldData);
    default:
      return Object.assign(Object.create(BaseField.prototype), fieldData);
  }
}

export { MultipleChoiceField as M, NumberField as N, ReferenceField as R, RangeField as a, generateFieldModelFromRawData as g };
