import { HTMLBuilder } from "@sankhyalabs/core";
import { CustomEditorSource } from "../../../../../utils/customEditor/interfaces/ICustomEditor";
import EzCellEditor from "../editor/EzCellEditor";
import { getViewPortHeight } from "../editor/GridEditorUtils";
import FocusResolver from "../../../../../utils/FocusResolver";
export default class EzGridCustomCellEditor extends EzCellEditor {
  init(params) {
    var _a, _b;
    this._value = params.value;
    this._fieldName = params.column.getColId();
    this._customEditorInstance = params.customEditorsInfo.customEditor;
    super.init(params);
    this._defaultGui = this.getDefaultGui();
    const fieldMetadata = super.getFieldMetadata();
    this._params = {
      cellPossition: getViewPortHeight(params.eGridCell),
      eGridCell: params.eGridCell,
      value: params.value,
      charPress: params.charPress,
      currentEditor: this._defaultGui,
      name: this._fieldName,
      source: CustomEditorSource.GRID,
      detailContext: params.customEditorsInfo.detailContext,
      setValue: (value) => this.setValue(value),
      getValue: () => this.getValue(),
      record: super.getRecord(),
      editorMetadata: {
        hidden: ((_a = fieldMetadata.props) === null || _a === void 0 ? void 0 : _a.visible) === false,
        label: fieldMetadata.label,
        userInterface: fieldMetadata.userInterface,
        props: fieldMetadata.props,
        optionLoader: fieldMetadata.optionLoader,
        options: ((_b = fieldMetadata.props) === null || _b === void 0 ? void 0 : _b.options) || {},
      },
    };
  }
  getDefaultGui() {
    const gui = super.getGui();
    gui.value = this._value;
    gui.onkeyup = (event) => {
      const value = event.target.value;
      this.setValue(value);
    };
    return gui;
  }
  getGui() {
    const editorElement = this._customEditorInstance.getEditorElement(this._params);
    if (!editorElement) {
      return this._defaultGui;
    }
    if (typeof editorElement === 'string') {
      const element = HTMLBuilder.parseElement(editorElement);
      return element;
    }
    this._customGui = editorElement;
    return editorElement;
  }
  setValue(value) {
    this._value = value;
  }
  getValue() {
    var _a, _b;
    if ((_a = this._customGui) === null || _a === void 0 ? void 0 : _a.getValue)
      return this._customGui.getValue();
    if ((_b = this._customGui) === null || _b === void 0 ? void 0 : _b.hasAttribute('value'))
      return this._customGui.value;
    return this._value;
  }
  afterGuiAttached() {
    this.focusIn();
  }
  focusIn() {
    FocusResolver.resolveFocus(this._customGui);
  }
}
