/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Item
 */
import * as React from "react";
import { Orientation } from "@itwin/core-react";
import { ItemDefBase } from "./ItemDefBase";
/** Abstract base class that is used by classes to execute an action when pressed.
 * @public
 */
export class ActionButtonItemDef extends ItemDefBase {
    constructor(itemProps, onItemExecuted) {
        super(itemProps);
        /** Called when the size of the action button is initialized and the size is known */
        this.handleSizeKnown = (size) => {
            this.size = size;
        };
        /** @internal */
        this.getKey = (index) => {
            const key = (!!this.id) ? this.id : (index !== undefined) ? index.toString() : ActionButtonItemDef.getRandomId();
            return key;
        };
        this.execute = this.execute.bind(this);
        this._onItemExecuted = onItemExecuted;
    }
    /** Called when the action button is invoked by a click or touch */
    execute() {
        if (this._commandHandler && this._commandHandler.execute) {
            if (this._commandHandler.getCommandArgs)
                this._commandHandler.execute(this._commandHandler.getCommandArgs());
            else
                this._commandHandler.execute(this._commandHandler.parameters);
        }
        // istanbul ignore else
        if (this._onItemExecuted)
            this._onItemExecuted(this);
    }
    /** Determines the dimension in a given orientation */
    getDimension(orientation) {
        let dimension = ActionButtonItemDef.defaultButtonSize;
        if (this.size)
            dimension = (orientation === Orientation.Horizontal) ? this.size.width : this.size.height;
        return dimension;
    }
    /** @internal */
    static getRandomId() {
        return (Math.floor(Math.random() * 100) + 1000).toString();
    }
    /** @internal */
    toolbarReactNode(index) {
        if (!this.isVisible) // eslint-disable-line deprecation/deprecation
            return null;
        const key = this.getKey(index);
        return (React.createElement(ActionItemButton // eslint-disable-line deprecation/deprecation
        , { key: key, actionItem: this, onSizeKnown: this.handleSizeKnown }));
    }
}
/** The default button size for all action buttons */
ActionButtonItemDef.defaultButtonSize = 42;
import { ActionItemButton } from "../toolbar/ActionItemButton";
//# sourceMappingURL=ActionButtonItemDef.js.map