/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

/**
 * Base class for all components implementing state management functionality.
 */
export class BaseComponent {
	/**
	 * Creates a base component with the given initial state.
	 *
	 * @param initialState - Initial state object for the component,
	 */
	constructor( initialState = {} ) {
		this.state = initialState;
	}

	/**
	 * Updates the component state and triggers a render.
	 *
	 * @param newState - Object containing state properties to update,
	 */
	setState( newState ) {
		const prevState = this.state;

		this.state = { ...this.state, ...newState };
		this.render( prevState, this.state );
	}

	/**
	 * Renders the component based on current state.
	 * This method should be overridden by child classes.
	 *
	 * @param prevState - Previous component state,
	 * @param state - Current component state,
	 */
	render() {
		throw new Error( 'Render method not implemented' );
	}
}
