/*!
 * KoliBri - The accessible HTML-Standard
 */
import { Log } from "../../../schema";
import { setFocus } from "../../../utils/element-focus";
import { createCtaRef } from "../../../utils/element-interaction";
import { nameProp } from "../../props";
import { BaseController } from "../base-controller";
import { skeletonPropsConfig } from "./api";
export 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;
        }
    }
}
//# sourceMappingURL=controller.js.map
