"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OjTreeView = void 0;
const OjTreeViewBase_1 = require("./OjTreeViewBase");
const __1 = require("../../");
const selenium_webdriver_1 = require("selenium-webdriver");
/**
 * The component WebElement for [oj-tree-view](../../jsdocs/oj.ojTreeView.html).
 * Do not instantiate this class directly, instead, use
 * [ojTreeView](../functions/elements.ojTreeView-1.html).
 */
class OjTreeView extends OjTreeViewBase_1.OjTreeViewBase {
    /**
     * Sets the value of "selected" property.
     * Specifies the current selected items in the treeview. See the Help documentation for more information.
     * @param selected The value to set for "selected"
     * @override
     * @typeparam K Type of keys
     */
    async changeSelected(selected) {
        await this.whenReady();
        await this.getDriver().executeScript(`
          const ele = arguments[0];
          const selected = arguments[1];
          const keySet = require('ojs/ojkeyset');
          ele.selected = new keySet.KeySetImpl(selected);
        `, this, selected);
    }
    /**
     * Gets the value of "selected" property.
     * Retrieves the current selected items in the treeview. See the Help documentation for more information.
     * @override
     * @typeparam K Type of keys
     * @return The value of "selected" property.
     */
    async getSelected() {
        await this.whenReady();
        const selected = await this.getDriver().executeScript(`
        const ele = arguments[0];
        const selected = Array.from(ele.selected.values())
        return selected;
      `, this);
        return selected;
    }
    /**
     * Retrieve a SlotProxy which represents a single treeview item.
     * @param key The key within the Collection's dataset associated with the item.
     * @param id The id of the li item, used when the treeview is static.
     */
    async findItem(itemLocator) {
        let item;
        if (itemLocator.key) {
            await this.whenBusyContextReady();
            item = await this.getDriver().executeAsyncScript(`
      var element = arguments[0];
      var key = arguments[1];
      var done = arguments[2];
      require([\'ojs/ojcore\'], function(oj) {
        const items = element.querySelectorAll('.oj-treeview-item');
        let item;
        for (var i = 0; i < items.length; i++) {
          if (oj.KeyUtils.equals(items[i]['oj-item-metadata'].key, key)) {
            item = items[i];
            break;
          }
        }
        if (item) {
         done(item.querySelector('.oj-treeview-item-content'));
        };
      });`, this, itemLocator.key);
        }
        else if (itemLocator.id) {
            await this.whenBusyContextReady();
            item = await this.getDriver().executeScript(`
      const item = document.getElementById(arguments[0]);
      if (item) {
        return item.querySelector('.oj-treeview-item-content');
      }`, itemLocator.id);
        }
        if (item) {
            return (0, __1.slotProxy)(item, this);
        }
        throw Error('No element found for given locator');
    }
    /**
     * Sets the value of "expanded" property.
     * Specifies the key set containing the keys of the items that should be expanded.
     * Use the ExpandedKeySet class to specify items to expand.
     * Use the ExpandAllKeySet class to expand all items.
     * @param expanded The value to set for "expanded"
     * @override
     */
    async changeExpanded(expanded) {
        await this.whenReady();
        await this.getDriver().executeScript(`
        const ele = arguments[0];
        const expanded  = arguments[1];
        const keySet = require('ojs/ojkeyset');
        const expandedKeySet = new keySet.ExpandedKeySet(expanded);
        ele.expanded = expandedKeySet;`, this, expanded);
        await this.whenBusyContextReady();
    }
    /**
     * Expands a collapsed treeview item. If already expanded it will remain expanded.
     * @param key The key of the item that is being expanded.
     * @param id The id of the li item to be expanded, used when the treeview is static.
     */
    async doExpand(itemLocator) {
        let itemContent = await this.findItem(itemLocator);
        let isExpanded = await this._isExpanded(itemContent);
        if (!isExpanded) {
            const ins = await this._getDisclosureIcon(itemContent);
            return ins.click();
        }
    }
    /**
     * Collapses an expanded treeview item. If already collapsed it will remain collapsed.
     * @param key The key of the item that is being collapsed.
     * @param id The id of the li item to be collapsed, used when the treeview is static.
     */
    async doCollapse(itemLocator) {
        let itemContent = await this.findItem(itemLocator);
        let isExpanded = await this._isExpanded(itemContent);
        if (isExpanded) {
            const ins = await this._getDisclosureIcon(itemContent);
            return ins.click();
        }
    }
    /**
     * Returns expanded state.
     * @param itemContent The itemContent WebElement that is being checked.
     */
    async _isExpanded(itemContent) {
        const ins = await this._getDisclosureIcon(itemContent);
        const driver = await ins.getDriver();
        return driver.executeScript("return arguments[0].parentNode.parentNode.classList.contains('oj-expanded')", ins);
    }
    /**
     * Returns the ins element.
     * @param itemContent The itemContent WebElement that contains the disclosure icon.
     */
    async _getDisclosureIcon(itemContent) {
        return itemContent.findElement(selenium_webdriver_1.By.css('INS'));
    }
}
exports.OjTreeView = OjTreeView;
//# sourceMappingURL=OjTreeView.js.map