import { ElementIDUtils } from "@sankhyalabs/core";
const executeAdvancedModeSearch = async (argument, options, app, pesquisaFetcher) => {
  const { entity, entityDescription, criteria, searchOptions, isHierarchyEntity, allowsNonAnalytic, executeOnOpen } = options;
  return new Promise((accept, reject) => {
    const pesquisaContent = document.createElement("snk-pesquisa");
    pesquisaContent[ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME] = `entity_${entity}`;
    pesquisaContent.entityName = entity;
    pesquisaContent.argument = argument;
    pesquisaContent.searchLoader = (text) => pesquisaFetcher.loadAdvancedSearch(entity, text, criteria, searchOptions);
    pesquisaContent.isHierarchyEntity = isHierarchyEntity;
    if (isHierarchyEntity) {
      pesquisaContent.treeLoader = (text) => pesquisaFetcher.loadTree(entity, text, criteria, searchOptions);
      pesquisaContent.allowsNonAnalytic = allowsNonAnalytic;
    }
    pesquisaContent.selectItem = (option) => {
      accept(option);
      app.clearPopUpTitle();
      app.closePopUp();
    };
    app.setPopUpTitle(entityDescription);
    app.showPopUp(pesquisaContent, undefined, undefined, reject);
    if (executeOnOpen) {
      pesquisaContent.executeSearch(argument);
    }
  });
};
export default executeAdvancedModeSearch;
