/*!
 * KoliBri - The accessible HTML-Standard
 */
import { h, proxyCustomElement, createEvent, Host } from '@stencil/core/internal/client';
import { _ as __decorate } from './tslib.es6.js';
import { B as BaseWebComponent } from './variant-quote.js';
import { b as bem } from './bem-registry.js';
import { C as ClickButtonFC } from './component4.js';
import './common.js';
import { L as Log } from './prop.validators.js';
import { s as setFocus } from './element-focus.js';
import { c as createCtaRef, d as delegateFocus } from './element-interaction.js';
import { c as createPropDefinition, n as normalizeString } from './normalizers.js';
import { B as BaseController } from './base-controller.js';

const nameProp = createPropDefinition('name', '', normalizeString);

const skeletonBem = bem.forBlock('kol-skeleton');
const BEM_CLASS_SKELETON__ACTIONS = skeletonBem('actions');
const BEM_CLASS_SKELETON__CONTAINER = skeletonBem('container');
const BEM_CLASS_SKELETON__COUNTER = skeletonBem('counter');
const BEM_CLASS_SKELETON__NAME = skeletonBem('name');
const SkeletonFC = (props) => {
    const { count, label, name, show, handleClick, refButton } = props;
    const hasName = !!(show && (name === null || name === void 0 ? void 0 : name.trim()));
    const BEM_CLASS_ROOT = skeletonBem({
        'has-name': hasName,
        'is-hidden': !show,
    });
    return (h("div", { class: BEM_CLASS_ROOT }, h("div", { class: BEM_CLASS_SKELETON__CONTAINER }, hasName && h("span", { class: BEM_CLASS_SKELETON__NAME }, name), h("div", { class: BEM_CLASS_SKELETON__COUNTER }, "Count: ", count)), h("div", { class: BEM_CLASS_SKELETON__ACTIONS }, h(ClickButtonFC, { label: label, handleClick: handleClick, refButton: refButton }))));
};

const skeletonPropsConfig = {
    required: [nameProp],
};

class SkeletonController extends BaseController {
    constructor(stateAccess) {
        super(stateAccess, skeletonPropsConfig);
        this.buttonRef = createCtaRef();
        this.onKeydown = (event) => {
            if (event.key === 'Escape') {
                console.log('Show should be toggled');
                void this.toggle();
            }
        };
        this.handleClick = () => {
            var _a, _b;
            Log.debug('Button clicked, count should be increased');
            this.setState('count', ((_b = (_a = this.getState) === null || _a === void 0 ? void 0 : _a.call(this, 'count')) !== null && _b !== void 0 ? _b : 0) + 1);
        };
        this.setButtonRef = (element) => {
            this.buttonRef(element);
        };
        this.startLoadedEventInterval();
    }
    componentWillLoad(props) {
        const { name } = props;
        this.watchName(name);
    }
    watchName(value) {
        nameProp.apply(value, (v) => {
            this.setRenderProp('name', v);
        });
    }
    toggle() {
        var _a, _b;
        this.setState('show', !((_b = (_a = this.getState) === null || _a === void 0 ? void 0 : _a.call(this, 'show')) !== null && _b !== void 0 ? _b : false));
    }
    focus(options) {
        void setFocus(this.buttonRef.el, options);
    }
    startLoadedEventInterval() {
        this.intervalId = setInterval(() => {
            var _a, _b;
            this.emitLoaded((_b = (_a = this.getState) === null || _a === void 0 ? void 0 : _a.call(this, 'count')) !== null && _b !== void 0 ? _b : 0);
        }, 2000);
    }
    emitLoaded(count) {
        if (this.onLoadedCallback) {
            this.onLoadedCallback(count);
        }
    }
    setOnLoadedCallback(callback) {
        this.onLoadedCallback = callback;
    }
    destroy() {
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = undefined;
        }
    }
}

const KolSkeleton$1 = proxyCustomElement(class KolSkeleton extends BaseWebComponent {
    constructor(registerHost) {
        super(false);
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.__attachShadow();
        this.loaded = createEvent(this, "loaded", 7);
        this.rendered = createEvent(this, "rendered", 7);
        this.ctrl = new SkeletonController(this.stateAccess);
        this.buttonRef = createCtaRef();
        this.count = 0;
        this.label = 'Label';
        this.show = true;
    }
    async focus(options) { }
    async toggle() {
        return Promise.resolve(this.ctrl.toggle());
    }
    watchName(value) {
        this.ctrl.watchName(value);
    }
    handleKeyDown(event) {
        if (event.key === 'Enter' || event.key === ' ') {
            Log.debug('button pressed');
            this.ctrl.handleClick();
        }
    }
    onKeydown(event) {
        this.ctrl.onKeydown(event);
    }
    componentWillLoad() {
        this.ctrl.componentWillLoad({
            name: this._name,
        });
        this.ctrl.setOnLoadedCallback((count) => {
            this.loaded.emit(count);
        });
    }
    componentDidLoad() {
        requestAnimationFrame(() => {
            this.rendered.emit();
        });
    }
    disconnectedCallback() {
        this.ctrl.destroy();
    }
    render() {
        return (h(Host, { key: 'e7e29ff558a0f2d6eebba4d8b2247ee269836bb5' }, h(SkeletonFC, { key: '949256a8455d79b2ea97182ecbf509fb0f3e59ee', count: this.count, label: this.label, name: this.ctrl.getRenderProp('name'), handleClick: this.ctrl.handleClick, onLoaded: this.loaded, onRendered: this.rendered, show: this.show, refButton: (element) => {
                this.buttonRef(element);
                this.ctrl.setButtonRef(element);
            } })));
    }
    static get watchers() { return {
        "_name": ["watchName"]
    }; }
}, [769, "kol-skeleton", {
        "_name": [1],
        "count": [32],
        "label": [32],
        "show": [32],
        "focus": [64],
        "toggle": [64]
    }, [[0, "keydown", "handleKeyDown"], [8, "keydown", "onKeydown"]], {
        "_name": ["watchName"]
    }]);
__decorate([
    delegateFocus('buttonRef')
], KolSkeleton$1.prototype, "focus", null);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["kol-skeleton"];
    components.forEach(tagName => { switch (tagName) {
        case "kol-skeleton":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, KolSkeleton$1);
            }
            break;
    } });
}

const KolSkeleton = KolSkeleton$1;
const defineCustomElement = defineCustomElement$1;

export { KolSkeleton, defineCustomElement };
//# sourceMappingURL=kol-skeleton.js.map

//# sourceMappingURL=kol-skeleton.js.map