import { HTMLBuilder } from "@sankhyalabs/core";
import { getViewPortHeight } from '../GridEditorUtils';
const BOOLEAN_OPTIONS = new Map([[true, { value: "S", label: "Sim" }], [false, { value: "N", label: "Não" }]]);
export const buildComboBox = ({ required, props, eGridCell }) => {
  const prop = props === null || props === void 0 ? void 0 : props.options;
  let options;
  if (typeof prop === "string") {
    const parsed = JSON.parse(prop);
    options = Object.keys(parsed).map(key => { return { value: key, label: parsed[key] }; });
  }
  else {
    options = prop;
  }
  const combo = HTMLBuilder.parseElement(`<ez-combo-box
        class="grid_editor"
        suppress-empty-option=${required}
        mode="slim"
    />`);
  combo.options = options;
  combo.stopPropagateEnterKeyEvent = true;
  combo.listOptionsPosition = { verticalPosition: 29, bottomLimit: getViewPortHeight(eGridCell), hardPosition: true };
  combo.autoFocus = true;
  return combo;
};
export const buildSwitch = (fieldMetadata) => {
  const combo = buildComboBox(Object.assign(Object.assign({}, fieldMetadata), { props: Object.assign(Object.assign({}, fieldMetadata.props), { options: Array.from(BOOLEAN_OPTIONS.values()) }) }));
  combo.valueGetter = () => {
    const value = combo.value;
    if (value == undefined) {
      return;
    }
    return value.value == "S";
  };
  combo.valueSetter = (value) => {
    if (value != undefined) {
      combo.value = BOOLEAN_OPTIONS.get(value);
    }
  };
  return combo;
};
