"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const dom_1 = require("@cloudscape-design/test-utils-core/dom");
const utils_dom_1 = require("@cloudscape-design/test-utils-core/utils-dom");
const index_js_1 = require("../button/index.js");
const styles_selectors_js_1 = require("../../../button/styles.selectors.js");
const styles_selectors_js_2 = require("../../../button-dropdown/category-elements/styles.selectors.js");
const styles_selectors_js_3 = require("../../../button-dropdown/item-element/styles.selectors.js");
const styles_selectors_js_4 = require("../../../button-dropdown/styles.selectors.js");
const styles_selectors_js_5 = require("../../../dropdown/styles.selectors.js");
const styles_selectors_js_6 = require("../../../internal/components/dropdown/styles.selectors.js");
function getItemSelector({ disabled }) {
    let selector = `.${styles_selectors_js_3.default['item-element']}`;
    if (disabled === true) {
        selector += `.${styles_selectors_js_3.default.disabled}`;
    }
    else if (disabled === false) {
        selector += `:not(.${styles_selectors_js_3.default.disabled})`;
    }
    return selector;
}
class ButtonDropdownWrapper extends dom_1.ComponentWrapper {
    findNativeButton() {
        return this.findByClassName(styles_selectors_js_4.default['dropdown-trigger']).findByClassName(styles_selectors_js_4.default['test-utils-button-trigger']);
    }
    findTriggerButton() {
        var _a, _b;
        return ((_b = (_a = this.findByClassName(styles_selectors_js_4.default['dropdown-trigger'])) === null || _a === void 0 ? void 0 : _a.findComponent(`.${styles_selectors_js_1.default.button}`, index_js_1.default)) !== null && _b !== void 0 ? _b : null);
    }
    findMainAction() {
        var _a, _b;
        return ((_b = (_a = this.findByClassName(styles_selectors_js_4.default['split-trigger'])) === null || _a === void 0 ? void 0 : _a.findComponent(`.${styles_selectors_js_1.default.button}`, index_js_1.default)) !== null && _b !== void 0 ? _b : null);
    }
    findOpenDropdown() {
        return (0, dom_1.createWrapper)().find(`:is(.${styles_selectors_js_5.default.dropdown}, .${styles_selectors_js_6.default.dropdown})[data-open=true]`);
    }
    /**
     * Finds an item in the open dropdown by item id. Returns null if there is no open dropdown.
     *
     * This utility does not open the dropdown. To find dropdown items, call `openDropdown()` first.
     *
     * Supported options:
     * * `disabled` (boolean) - Use it to find the disabled or non-disabled item.
     */
    findItemById(id, options = {}) {
        var _a;
        const itemSelector = `${getItemSelector(options)}[data-testid="${id}"]`;
        return ((_a = this.findOpenDropdown()) === null || _a === void 0 ? void 0 : _a.find(itemSelector)) || this.find(itemSelector);
    }
    /**
     * Finds `checked` value of item in the open dropdown by item id. Returns null if there is no open dropdown or the item is not a checkbox item.
     *
     * This utility does not open the dropdown. To find dropdown items, call `openDropdown()` first.
     */
    findItemCheckedById(id) {
        var _a;
        const itemSelector = `.${styles_selectors_js_3.default['item-element']}[data-testid="${id}"]`;
        const item = ((_a = this.findOpenDropdown()) === null || _a === void 0 ? void 0 : _a.find(itemSelector)) || this.find(itemSelector);
        if (!item) {
            return null;
        }
        const menuItem = item.find('[role="menuitemcheckbox"]');
        if (!menuItem) {
            return null;
        }
        return menuItem.getElement().getAttribute('aria-checked');
    }
    /**
     * Finds an expandable category in the open dropdown by category id. Returns null if there is no open dropdown.
     *
     * This utility does not open the dropdown. To find dropdown items, call `openDropdown()` first.
     */
    findExpandableCategoryById(id) {
        var _a;
        const expandableCategorySelector = `.${styles_selectors_js_2.default.expandable}[data-testid="${id}"]`;
        return ((_a = this.findOpenDropdown()) === null || _a === void 0 ? void 0 : _a.find(expandableCategorySelector)) || this.find(expandableCategorySelector);
    }
    /**
     * Finds the highlighted item in the open dropdown. Returns null if there is no open dropdown.
     *
     * This utility does not open the dropdown. To find dropdown items, call `openDropdown()` first.
     */
    findHighlightedItem() {
        var _a;
        const highlightedItemSelector = `.${styles_selectors_js_3.default['item-element']}.${styles_selectors_js_3.default.highlighted}`;
        return ((_a = this.findOpenDropdown()) === null || _a === void 0 ? void 0 : _a.find(highlightedItemSelector)) || this.find(highlightedItemSelector);
    }
    /**
     * Finds all the items in the open dropdown. Returns empty array if there is no open dropdown.
     *
     * This utility does not open the dropdown. To find dropdown items, call `openDropdown()` first.
     *
     * Supported options:
     * * `disabled` (boolean) - Use it to find all disabled or non-disabled items.
     */
    findItems(options = {}) {
        var _a;
        const selector = getItemSelector(options);
        return ((_a = this.findOpenDropdown()) === null || _a === void 0 ? void 0 : _a.findAll(selector)) || [];
    }
    /**
     * Finds the disabled reason tooltip for a dropdown item. Returns null if no disabled item with `disabledReason` is highlighted.
     */
    findDisabledReason() {
        return (0, dom_1.createWrapper)().find(`[data-testid="button-dropdown-disabled-reason"]`);
    }
    openDropdown() {
        (0, utils_dom_1.act)(() => {
            this.findNativeButton().click();
        });
    }
}
ButtonDropdownWrapper.rootSelector = styles_selectors_js_4.default['button-dropdown'];
exports.default = ButtonDropdownWrapper;
__decorate([
    dom_1.usesDom
], ButtonDropdownWrapper.prototype, "findItemCheckedById", null);
__decorate([
    dom_1.usesDom
], ButtonDropdownWrapper.prototype, "openDropdown", null);
//# sourceMappingURL=index.js.map