import { ButtonConfig, Button } from './Button';
import { UIInstanceManager } from '../../UIManager';
import { Component, ComponentConfig } from '../Component';
import { PlayerAPI } from 'bitmovin-player';
import { i18n } from '../../localization/i18n';

/**
 * Configuration interface for the {@link CloseButton}.
 *
 * @category Configs
 */
export interface CloseButtonConfig extends ButtonConfig {
  /**
   * The component that should be closed when the button is clicked.
   */
  target: Component<ComponentConfig>;
}

/**
 * A button that closes (hides) a configured component.
 *
 * @category Buttons
 */
export class CloseButton extends Button<CloseButtonConfig> {
  constructor(config: CloseButtonConfig) {
    super(config);

    this.config = this.mergeConfig(
      config,
      {
        cssClass: 'ui-closebutton',
        text: i18n.getLocalizer('close'),
      } as CloseButtonConfig,
      this.config,
    );
  }

  configure(player: PlayerAPI, uimanager: UIInstanceManager): void {
    super.configure(player, uimanager);

    const config = this.getConfig();

    this.onClick.subscribe(() => {
      config.target.hide();
    });
  }
}
