'use strict';

var index = require('./index-Bc25FxCt.js');

/**
 * Sub state notify slimmer
 */
class SubStateNotifySlimmerBase extends index.VegaSlimmer {
    /**
     * Register the observer in connectedCallback
     */
    connectedCallback() {
        this.subStateObserver = new index.Observer(this.isAcceptableObserverPayload.bind(this), (payload) => {
            payload.callback(this.propValue);
        });
        index.ChangeManager.register(index.domNodeSubjectFactory.getSubject(this.host, index.VegaInternalSubStatePull), this.subStateObserver);
    }
    /**
     * unregister the observer in disconnectedCallback
     */
    disconnectedCallback() {
        index.ChangeManager.unregister(index.domNodeSubjectFactory.getSubject(this.host, index.VegaInternalSubStatePull), this.subStateObserver);
    }
    // eslint-disable-next-line jsdoc/require-jsdoc
    constructor({ propName }) {
        super();
        /**
         * Checks if the input `host` matches the current `host` and `stateName` matches the current `propName`.
         *
         * @param {unknown} input - The `input` parameter is of type `unknown`, which means it can be any type.
         * @returns {boolean} a boolean value indicating whether the input is an acceptable table observer.
         */
        this.isAcceptableObserverPayload = (input) => {
            return input.host === this.host && input.stateName === this.propName;
        };
        this.propName = propName;
    }
    /**
     * Notify the children component which prop has been changed in the parent component
     */
    subStateNotify() {
        index.ChangeManager.notify(index.domNodeSubjectFactory.getSubject(this.host, index.VegaInternalSubStatePush), {
            host: this.host,
            stateName: this.propName,
            value: this.propValue,
        });
    }
}

/**
 * Create dynamic subState notify slimmer
 *
 * @param {ClassPrototype} mapFromClass - It represents the class that you want to map from.
 * @param {string} propName - The parent component prop name
 * @param {string} watchMethodName - The watch method name of prop change
 * @returns {SubStateNotifySlimmerBase} - SubState notify slimmer
 */
function createSubStateNotifySlimmer(mapFromClass, propName, watchMethodName) {
    const methodsMap = [
        { mapFrom: 'connectedCallback', mapTo: 'connectedCallback' },
        { mapFrom: 'disconnectedCallback', mapTo: 'disconnectedCallback' },
    ];
    if (watchMethodName) {
        methodsMap.push({ mapFrom: watchMethodName, mapTo: 'subStateNotify' });
    }
    return index.createDynamicSlimmer(mapFromClass, SubStateNotifySlimmerBase, [
        { mapFrom: 'host', mapTo: 'host' },
        { mapFrom: propName, mapTo: 'propValue' },
    ], methodsMap, [
        {
            propName: propName,
        },
    ]);
}

exports.createSubStateNotifySlimmer = createSubStateNotifySlimmer;
