/*!
 * KoliBri - The accessible HTML-Standard
 */
'use strict';

var common = require('./common-DAAt5YQ7.js');
var label = require('./label-VzToxIES.js');
var required = require('./required-QyEHP_gi.js');
var orientation = require('./orientation-BUQ3VvMw.js');
var FormFieldStateWrapper = require('./FormFieldStateWrapper-C93KLp60.js');

const validateInputSelectOptions = (option) => {
    if (typeof option === 'object' && option !== null) {
        if (typeof option.label === 'string' && option.label.length > 0) {
            option.disabled = option.disabled === true;
            option.label = `${option.label}`.trim();
            if (label.hasEnoughReadableChars(option.label, 3) === false && label.containsOnlyNumbers(option.label) === false) {
                common.a11yHint(`A differing Aria-Label (${option.label}) is inaccessible. A differing Aria-Label should consist of at least three readable characters.`);
            }
            if (Array.isArray(option.options)) {
                return (option.options.find((item) => {
                    return validateInputSelectOptions(item) === false;
                }) === undefined);
            }
            return true;
        }
        else if (typeof option.label === 'number') {
            return true;
        }
    }
    return false;
};

const validateOptions = (component, value, options = {}) => {
    common.watchJsonArrayString(component, '_options', (item) => typeof item === 'object' && item !== null && typeof item.label === 'string' && item.label.length > 0, value, undefined, options);
};
const validateOptionsWithOptgroup = (component, value, options = {}) => {
    common.watchJsonArrayString(component, '_options', validateInputSelectOptions, value, undefined, options);
};

const fillKeyOptionMap = (keyOptionMap, options, preKey = '') => {
    options.forEach((option, index) => {
        const key = `${preKey}-${index}`;
        if (typeof option === 'object' && option !== null && typeof option.label === 'string' && option.label.length > 0) {
            if (Array.isArray(option.options)) {
                fillKeyOptionMap(keyOptionMap, option.options, key);
            }
            else {
                keyOptionMap.set(key, option);
            }
        }
    });
};
class InputCheckboxRadioController extends FormFieldStateWrapper.InputController {
    constructor(component, name, host) {
        super(component, name, host);
        this.component = component;
    }
    validateRequired(value) {
        required.validateRequired(this.component, value);
    }
    componentWillLoad() {
        super.componentWillLoad();
        this.validateRequired(this.component._required);
    }
}
class InputRadioController extends InputCheckboxRadioController {
    constructor(component, name, host) {
        super(component, name, host);
        this.keyOptionMap = new Map();
        this.getOptionByKey = (key) => this.keyOptionMap.get(key);
        this.afterPatchOptions = (value, _state, _component, key) => {
            if (key === '_value') {
                this.setFormAssociatedValue(value);
            }
        };
        this.beforePatchOptions = (_value, nextState) => {
            const options = nextState.has('_options') ? nextState.get('_options') : this.component.state._options;
            if (Array.isArray(options) && options.length > 0) {
                this.keyOptionMap.clear();
                const normalizedOptions = options.map((option) => {
                    var _a;
                    if (typeof option === 'object' && option !== null && typeof option.label === 'string') {
                        return Object.assign(Object.assign({}, option), { value: (_a = option.value) !== null && _a !== void 0 ? _a : option.label });
                    }
                    return option;
                });
                fillKeyOptionMap(this.keyOptionMap, normalizedOptions);
            }
        };
        this.component = component;
    }
    validateOrientation(value) {
        orientation.validateOrientation(this.component, value, 'vertical');
    }
    validateOptions(value) {
        validateOptions(this.component, value, {
            hooks: {
                afterPatch: this.afterPatchOptions,
                beforePatch: this.beforePatchOptions,
            },
        });
    }
    validateValue(value) {
        value = Array.isArray(value) ? value[0] : value;
        common.setState(this.component, '_value', value, {
            afterPatch: this.afterPatchOptions,
            beforePatch: this.beforePatchOptions,
        });
    }
    componentWillLoad() {
        super.componentWillLoad();
        this.validateOrientation(this.component._orientation);
        this.validateOptions(this.component._options);
        this.validateValue(this.component._value);
    }
}

exports.InputCheckboxRadioController = InputCheckboxRadioController;
exports.InputRadioController = InputRadioController;
exports.fillKeyOptionMap = fillKeyOptionMap;
exports.validateOptions = validateOptions;
exports.validateOptionsWithOptgroup = validateOptionsWithOptgroup;
//# sourceMappingURL=controller-P5D-ZKaP.js.map

//# sourceMappingURL=controller-P5D-ZKaP.js.map