import { UserInterface } from "@sankhyalabs/core";
import { buildEditorMetadata } from "./IEditorMetadata";
import { buildTextAreaInput, buildTextInput } from "./templates/TextInput.tpl";
import { buildDate, buildDateTime, buildTime } from "./templates/DateInput.tpl";
import { buildDecimal, buildInteger } from "./templates/NumberInput.tpl";
import { buildComboBox, buildSwitch } from "./templates/ComboBox.tpl";
import { buildSearchPlus } from "./templates/SearchPlus.tpl";
import { buildSearch } from "./templates/Search.tpl";
const uiBuilders = new Map();
uiBuilders.set(UserInterface.DATE, buildDate);
uiBuilders.set(UserInterface.TIME, buildTime);
uiBuilders.set(UserInterface.DATETIME, buildDateTime);
uiBuilders.set(UserInterface.DECIMALNUMBER, buildDecimal);
uiBuilders.set(UserInterface.INTEGERNUMBER, buildInteger);
uiBuilders.set(UserInterface.SWITCH, buildSwitch);
uiBuilders.set(UserInterface.CHECKBOX, buildSwitch);
uiBuilders.set(UserInterface.OPTIONSELECTOR, buildComboBox);
uiBuilders.set(UserInterface.LONGTEXT, buildTextAreaInput);
uiBuilders.set(UserInterface.SEARCH, buildSearch);
uiBuilders.set(UserInterface.SEARCHPLUS, buildSearchPlus);
export default class EzCellEditor {
  init(params) {
    this._rowData = params.data;
    const fieldName = params.column.getColId();
    const dataUnit = params.context.dataUnit;
    this._fieldMetadata = buildEditorMetadata(dataUnit.getField(fieldName), params.eGridCell, dataUnit, params.context.editionManager);
    const builder = uiBuilders.get(this._fieldMetadata.userInterface) || buildTextInput;
    this._gui = builder(this._fieldMetadata);
    if (this._gui.valueSetter != undefined) {
      this._gui.valueSetter(params.value);
    }
    else {
      this._gui.value = params.value;
    }
  }
  getGui() {
    return this._gui;
  }
  afterGuiAttached() {
    this.focusIn();
  }
  focusIn() {
    this._gui.focus();
    this._gui.setFocus({ selectText: true });
  }
  getValue() {
    return this._gui.valueGetter != undefined ? this._gui.valueGetter() : this._gui.value;
  }
  setGuiValue(value) {
    if (this._gui.valueSetter != undefined) {
      this._gui.valueSetter(value);
    }
    else {
      this._gui.value = value;
    }
  }
  isPopup() {
    return this._gui.isPopUp;
  }
  getFieldMetadata() {
    return this._fieldMetadata;
  }
  getRecord() {
    return this._rowData;
  }
}
