/*!
 * KoliBri - The accessible HTML-Standard
 */
import { objectObjectHandler, parseJson, watchValidator } from "../utils";
import { isObject, isString, isStyle } from "../validators";
const mapCustomIcon = (state, alignment, icon) => {
    if (isObject(icon)) {
        state[alignment] = icon;
    }
    else if (isString(icon, 1)) {
        state[alignment] = {
            icon: icon,
        };
    }
};
export const mapIconProp2State = (icon) => {
    let state = {};
    if (isString(icon, 1)) {
        state = {
            left: {
                icon: icon,
            },
        };
    }
    else if (typeof icon === 'object' && icon !== null) {
        mapCustomIcon(state, 'top', icon.top);
        mapCustomIcon(state, 'right', icon.right);
        mapCustomIcon(state, 'bottom', icon.bottom);
        mapCustomIcon(state, 'left', icon.left);
    }
    return state;
};
const beforePatchIcon = (component) => {
    var _a, _b, _c;
    if ((_a = component.nextState) === null || _a === void 0 ? void 0 : _a.has('_icons')) {
        const icons = (_b = component.nextState) === null || _b === void 0 ? void 0 : _b.get('_icons');
        (_c = component.nextState) === null || _c === void 0 ? void 0 : _c.set('_icons', mapIconProp2State(icons));
    }
};
export const isIcon = (value) => typeof value === 'object' &&
    value !== null &&
    (typeof value.style === 'undefined' || isStyle(value.style)) &&
    (typeof value.label === 'undefined' || isString(value.label)) &&
    isString(value.icon, 1);
export const validateIcons = (component, value, options = {}) => {
    objectObjectHandler(value, () => {
        var _a;
        try {
            value = parseJson(value);
        }
        catch (_b) {
        }
        watchValidator(component, '_icons', (value) => {
            const valueIsEmptyObject = typeof value === 'object' && value !== null && Object.keys(value).length === 0;
            return (value === null ||
                valueIsEmptyObject ||
                isString(value, 1) ||
                (typeof value === 'object' &&
                    value !== null &&
                    (isString(value.left, 0) ||
                        isIcon(value.left) ||
                        isString(value.right, 0) ||
                        isIcon(value.right) ||
                        isString(value.top, 0) ||
                        isIcon(value.top) ||
                        isString(value.bottom, 0) ||
                        isIcon(value.bottom))));
        }, new Set(['KoliBriIcon']), value, Object.assign(Object.assign({}, options), { defaultValue: {}, hooks: {
                afterPatch: (_a = options.hooks) === null || _a === void 0 ? void 0 : _a.afterPatch,
                beforePatch: (nextValue, nextState, component, key) => {
                    var _a, _b;
                    if (typeof ((_a = options.hooks) === null || _a === void 0 ? void 0 : _a.beforePatch) === 'function') {
                        (_b = options.hooks) === null || _b === void 0 ? void 0 : _b.beforePatch(nextValue, nextState, component, key);
                    }
                    beforePatchIcon(component);
                },
            } }));
    });
};
//# sourceMappingURL=icons.js.map
