/**
 * Copyright IBM Corp. 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import "../../globals/settings.js";
import { carbonElement } from "../../globals/decorators/carbon-element.js";
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.126.0/helpers/decorate.js";
import data_table_default from "./data-table.scss.js";
import { LitElement, html, nothing } from "lit";
import { property } from "lit/decorators.js";
//#region src/components/data-table/table-batch-actions.ts
/**
* Copyright IBM Corp. 2019, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var _CDSTableBatchActions;
/**
* Table batch actions.
*
* @element cds-table-batch-actions
* @fires cds-table-batch-actions-cancel-clicked - The custom event fired after the Cancel button is clicked.
* @fires cds-table-batch-actions-select-all-clicked - The custom event fired after the Select all button is clicked.
*/
let CDSTableBatchActions = class CDSTableBatchActions extends LitElement {
	static {
		_CDSTableBatchActions = this;
	}
	constructor(..._args) {
		super(..._args);
		this.active = false;
		this.formatSelectedItemsCount = ({ count }) => `${count} item${count <= 1 ? "" : "s"} selected`;
		this.selectedRowsCount = 0;
		this.totalRowsCount = 0;
		this.size = "lg";
	}
	/**
	* Handles `click` event on the Cancel button.
	*/
	_handleCancel() {
		const { eventClickCancel } = this.constructor;
		this.dispatchEvent(new CustomEvent(eventClickCancel, {
			bubbles: true,
			composed: true
		}));
	}
	/**
	* Handles `click` event on the Select all button.
	*/
	_handleSelectAll() {
		this.dispatchEvent(new CustomEvent(_CDSTableBatchActions.eventClickSelectAll, {
			bubbles: true,
			composed: true
		}));
	}
	firstUpdated() {
		this._updateButtons();
		this._setupHoverListeners();
	}
	_setupHoverListeners() {
		const slot = this.shadowRoot?.querySelector("slot");
		const cancelButton = this.shadowRoot?.querySelector(`cds-button.cds--batch-summary__cancel`);
		if (slot && cancelButton) {
			const setupListeners = () => {
				const buttons = slot.assignedElements();
				const lastButton = buttons[buttons.length - 1];
				if (lastButton) {
					const handleEnter = () => {
						cancelButton.style.setProperty("--divider-opacity", "0");
					};
					const handleLeave = () => {
						cancelButton.style.setProperty("--divider-opacity", "1");
					};
					lastButton.removeEventListener("mouseenter", handleEnter);
					lastButton.removeEventListener("mouseleave", handleLeave);
					lastButton.addEventListener("mouseenter", handleEnter);
					lastButton.addEventListener("mouseleave", handleLeave);
				}
			};
			setupListeners();
			slot.addEventListener("slotchange", setupListeners);
		}
	}
	updated(changedProperties) {
		if (changedProperties.has("active")) this.setAttribute("tabindex", `${this.active ? "" : "-1"}`);
		if (changedProperties.has("size")) this._updateButtons();
	}
	_updateButtons() {
		this.querySelectorAll(this.constructor.selectorButtons).forEach((button) => {
			button.setAttribute("batch-action", "");
			const buttonSize = this.size === "xs" || this.size === "sm" ? "sm" : "lg";
			button.setAttribute("size", buttonSize);
		});
	}
	render() {
		const { formatSelectedItemsCount, selectedRowsCount, totalRowsCount, _handleCancel: handleCancel, _handleSelectAll: handleSelectAll, size } = this;
		const buttonSize = size === "xs" || size === "sm" ? "sm" : "lg";
		return html`
      <div class="${"cds"}--batch-summary">
        <p class="${"cds"}--batch-summary__para">
          ${formatSelectedItemsCount({ count: selectedRowsCount })}
        </p>
        ${totalRowsCount > 0 ? html`
              <span class="${"cds"}--batch-summary__divider">|</span>
              <button
                class="${"cds"}--btn ${"cds"}--btn--primary ${"cds"}--batch-summary__select-all"
                @click=${handleSelectAll}>
                <slot name="select-all-button-content"
                  >Select all (${totalRowsCount})</slot
                >
              </button>
            ` : nothing}
      </div>

      <div class="${"cds"}--action-list">
        <slot></slot>
        <cds-button
          kind="primary"
          size="${buttonSize}"
          class="${"cds"}--batch-summary__cancel"
          batch-action
          @click=${handleCancel}>
          <slot name="cancel-button-content">Cancel</slot>
        </cds-button>
      </div>
    `;
	}
	/**
	* The CSS selector to find the action buttons.
	*/
	static get selectorButtons() {
		return `cds-button`;
	}
	/**
	* The name of the custom event fired after the Cancel button is clicked.
	*/
	static get eventClickCancel() {
		return `cds-table-batch-actions-cancel-clicked`;
	}
	/**
	* The name of the custom event fired after the Select all button is clicked.
	*/
	static get eventClickSelectAll() {
		return `cds-table-batch-actions-select-all-clicked`;
	}
	static {
		this.styles = data_table_default;
	}
};
__decorate([property({
	type: Boolean,
	reflect: true
})], CDSTableBatchActions.prototype, "active", void 0);
__decorate([property({ attribute: false })], CDSTableBatchActions.prototype, "formatSelectedItemsCount", void 0);
__decorate([property({
	type: Number,
	attribute: "selected-rows-count"
})], CDSTableBatchActions.prototype, "selectedRowsCount", void 0);
__decorate([property({
	type: Number,
	attribute: "total-rows-count"
})], CDSTableBatchActions.prototype, "totalRowsCount", void 0);
__decorate([property({ reflect: true })], CDSTableBatchActions.prototype, "size", void 0);
CDSTableBatchActions = _CDSTableBatchActions = __decorate([carbonElement(`cds-table-batch-actions`)], CDSTableBatchActions);
var table_batch_actions_default = CDSTableBatchActions;
//#endregion
export { table_batch_actions_default as default };

//# sourceMappingURL=table-batch-actions.js.map