import { Arr, Future, Result } from '@ephox/katamari';
import { Attribute, Class, Html, SugarNode } from '@ephox/sugar';
const ariaElements = [
    'input',
    'textarea'
];
const isAriaElement = (elem) => {
    const name = SugarNode.name(elem);
    return Arr.contains(ariaElements, name);
};
const markValid = (component, invalidConfig) => {
    const elem = invalidConfig.getRoot(component).getOr(component.element);
    Class.remove(elem, invalidConfig.invalidClass);
    invalidConfig.notify.each((notifyInfo) => {
        if (isAriaElement(component.element)) {
            Attribute.set(component.element, 'aria-invalid', false);
        }
        notifyInfo.getContainer(component).each((container) => {
            Html.set(container, notifyInfo.validHtml);
        });
        notifyInfo.onValid(component);
    });
};
const markInvalid = (component, invalidConfig, invalidState, text) => {
    const elem = invalidConfig.getRoot(component).getOr(component.element);
    Class.add(elem, invalidConfig.invalidClass);
    invalidConfig.notify.each((notifyInfo) => {
        if (isAriaElement(component.element)) {
            Attribute.set(component.element, 'aria-invalid', true);
        }
        notifyInfo.getContainer(component).each((container) => {
            // TODO: Should we just use Text here, not HTML?
            Html.set(container, text);
        });
        notifyInfo.onInvalid(component, text);
    });
};
const query = (component, invalidConfig, _invalidState) => invalidConfig.validator.fold(() => Future.pure(Result.value(true)), (validatorInfo) => validatorInfo.validate(component));
const run = (component, invalidConfig, invalidState) => {
    invalidConfig.notify.each((notifyInfo) => {
        notifyInfo.onValidate(component);
    });
    return query(component, invalidConfig, invalidState).map((valid) => {
        if (component.getSystem().isConnected()) {
            return valid.fold((err) => {
                markInvalid(component, invalidConfig, invalidState, err);
                return Result.error(err);
            }, (v) => {
                markValid(component, invalidConfig);
                return Result.value(v);
            });
        }
        else {
            return Result.error('No longer in system');
        }
    });
};
const isInvalid = (component, invalidConfig) => {
    const elem = invalidConfig.getRoot(component).getOr(component.element);
    return Class.has(elem, invalidConfig.invalidClass);
};
export { markValid, markInvalid, query, run, isInvalid };
//# sourceMappingURL=InvalidateApis.js.map