import { By } from 'selenium-webdriver';
import { OjCollapsibleBase } from './OjCollapsibleBase';

/**
 * The component WebElement for [oj-collapsible](../../jsdocs/oj.ojCollapsible.html).
 * Do not instantiate this class directly, instead, use
 * [ojCollapsible](../functions/elements.ojCollapsible-1.html).
 */
export class OjCollapsible extends OjCollapsibleBase {
  /**
   * Expand the oj-collapsible. If the component is collapsed, this method will
   * expand it. If it's already expanded, this method will do nothing.
   */
  async doExpand() {
    const expanded = await this.getExpanded();
    if (!expanded) {
      let icon = await this.findElement(By.css('a.oj-collapsible-header-icon'));
      await icon.click();
    }
  }

  /**
   * Collapse the oj-collapsible. If the component is expanded, this method will
   * collapse it. If it's already collapsed, this method will do nothing.
   */
  async doCollapse() {
    const expanded = await this.getExpanded();
    if (expanded) {
      let icon = await this.findElement(By.css('a.oj-collapsible-header-icon'));
      await icon.click();
    }
  }

  /**
   * Sets the value of <code>expanded</code> property.
   * Specifies if the content is expanded.
   * @param expanded The value to set for <code>expanded</code>
   * @deprecated Since 13.0.0. Use doCollapse/doExpand instead
   *
   */
  public changeExpanded(expanded: boolean): Promise<void> {
    return this.setProperty<boolean>('expanded', expanded);
  }
}
