import hideButKeepRequireBehavior from "../util/hide_but_required_behavior";

const handler = (el, defaultOptions, libs) => {
    let ChoicesJs = libs.getLib('choicesjs');
    let unsorted = el && el.classList.contains('aa-ft-unsorted');
    var aaFormComponent = new ChoicesJs(el, {
        ...defaultOptions,
        ...(unsorted === true ? {shouldSort: false,} : {})
    });
    hideButKeepRequireBehavior(el);
    el.removeAttribute('hidden');// bothering style on this attribute, both in bootstrap and choices.js styles

    return aaFormComponent;
};

AAForm.addTypeHandler('aa-ft-select-rich', handler)
AAForm.setTypeDefaultOptions('aa-ft-select-rich', AAFormConfig.config['aa-ft-select-rich'])

AAForm.addTypeHandler('aa-ft-rich-entity', handler)
AAForm.setTypeDefaultOptions('aa-ft-rich-entity', AAFormConfig.config['aa-ft-rich-entity'])
