import { __esDecorate, __runInitializers } from "tslib";
import { html } from 'lit';
import { property, query } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { UiElement } from '../../UiElement.js';
import { setDisabled } from '../../../lib/disabled.js';
import { ripple } from '../../effects/rippleDirective.js';
import '@material/web/focus/md-focus-ring.js';
import '../../ripple/ui-ripple.js';
export var ListItemLines;
(function (ListItemLines) {
    ListItemLines["one"] = "one";
    ListItemLines["two"] = "two";
    ListItemLines["three"] = "three";
})(ListItemLines || (ListItemLines = {}));
export var ListItemImage;
(function (ListItemImage) {
    ListItemImage["icon"] = "icon";
    ListItemImage["avatar"] = "avatar";
    ListItemImage["image"] = "image";
    ListItemImage["video"] = "video";
    ListItemImage["auto"] = "auto";
})(ListItemImage || (ListItemImage = {}));
let UiListItem = (() => {
    let _classSuper = UiElement;
    let _ripple_decorators;
    let _ripple_initializers = [];
    let _ripple_extraInitializers = [];
    let _disabled_decorators;
    let _disabled_initializers = [];
    let _disabled_extraInitializers = [];
    let _lines_decorators;
    let _lines_initializers = [];
    let _lines_extraInitializers = [];
    let _image_decorators;
    let _image_initializers = [];
    let _image_extraInitializers = [];
    let _static_decorators;
    let _static_initializers = [];
    let _static_extraInitializers = [];
    let _tabIndex_decorators;
    let _tabIndex_initializers = [];
    let _tabIndex_extraInitializers = [];
    return class UiListItem extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _ripple_decorators = [query('ui-ripple')];
            _disabled_decorators = [property({ type: Boolean, reflect: true })];
            _lines_decorators = [property({ type: String, reflect: true })];
            _image_decorators = [property({ type: String, reflect: true })];
            _static_decorators = [property({ type: Boolean, reflect: true })];
            _tabIndex_decorators = [property({ type: Number, reflect: true })];
            __esDecorate(this, null, _ripple_decorators, { kind: "accessor", name: "ripple", static: false, private: false, access: { has: obj => "ripple" in obj, get: obj => obj.ripple, set: (obj, value) => { obj.ripple = value; } }, metadata: _metadata }, _ripple_initializers, _ripple_extraInitializers);
            __esDecorate(this, null, _disabled_decorators, { kind: "accessor", name: "disabled", static: false, private: false, access: { has: obj => "disabled" in obj, get: obj => obj.disabled, set: (obj, value) => { obj.disabled = value; } }, metadata: _metadata }, _disabled_initializers, _disabled_extraInitializers);
            __esDecorate(this, null, _lines_decorators, { kind: "accessor", name: "lines", static: false, private: false, access: { has: obj => "lines" in obj, get: obj => obj.lines, set: (obj, value) => { obj.lines = value; } }, metadata: _metadata }, _lines_initializers, _lines_extraInitializers);
            __esDecorate(this, null, _image_decorators, { kind: "accessor", name: "image", static: false, private: false, access: { has: obj => "image" in obj, get: obj => obj.image, set: (obj, value) => { obj.image = value; } }, metadata: _metadata }, _image_initializers, _image_extraInitializers);
            __esDecorate(this, null, _static_decorators, { kind: "accessor", name: "static", static: false, private: false, access: { has: obj => "static" in obj, get: obj => obj.static, set: (obj, value) => { obj.static = value; } }, metadata: _metadata }, _static_initializers, _static_extraInitializers);
            __esDecorate(this, null, _tabIndex_decorators, { kind: "accessor", name: "tabIndex", static: false, private: false, access: { has: obj => "tabIndex" in obj, get: obj => obj.tabIndex, set: (obj, value) => { obj.tabIndex = value; } }, metadata: _metadata }, _tabIndex_initializers, _tabIndex_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #ripple_accessor_storage = __runInitializers(this, _ripple_initializers, void 0);
        get ripple() { return this.#ripple_accessor_storage; }
        set ripple(value) { this.#ripple_accessor_storage = value; }
        #disabled_accessor_storage = (__runInitializers(this, _ripple_extraInitializers), __runInitializers(this, _disabled_initializers, false
        /**
         * The number of lines to render the list template for.
         * @default ListItemLines.one
         * @attribute
         */
        ));
        /**
         * Whether the chip is disabled. The user can't interact with the chip when `true`.
         * @attribute
         */
        get disabled() { return this.#disabled_accessor_storage; }
        set disabled(value) { this.#disabled_accessor_storage = value; }
        #lines_accessor_storage = (__runInitializers(this, _disabled_extraInitializers), __runInitializers(this, _lines_initializers, void 0));
        /**
         * The number of lines to render the list template for.
         * @default ListItemLines.one
         * @attribute
         */
        get lines() { return this.#lines_accessor_storage; }
        set lines(value) { this.#lines_accessor_storage = value; }
        #image_accessor_storage = (__runInitializers(this, _lines_extraInitializers), __runInitializers(this, _image_initializers, void 0));
        /**
         * The type of the list image.
         * @default ListItemImage.icon
         * @attribute
         */
        get image() { return this.#image_accessor_storage; }
        set image(value) { this.#image_accessor_storage = value; }
        #static_accessor_storage = (__runInitializers(this, _image_extraInitializers), __runInitializers(this, _static_initializers, false));
        /**
         * Whether the list item is static and should not be focusable.
         * The ripple effect will not be applied when `true`.
         * @default false
         * @attribute
         */
        get static() { return this.#static_accessor_storage; }
        set static(value) { this.#static_accessor_storage = value; }
        #tabIndex_accessor_storage = (__runInitializers(this, _static_extraInitializers), __runInitializers(this, _tabIndex_initializers, -1));
        get tabIndex() { return this.#tabIndex_accessor_storage; }
        set tabIndex(value) { this.#tabIndex_accessor_storage = value; }
        constructor() {
            super();
            this.lines = ListItemLines.one;
            this.image = ListItemImage.icon;
            this.addEventListener('click', this.handleClick.bind(this));
            this.addEventListener('pointerdown', this.handlePointerDown.bind(this));
            this.addEventListener('pointerup', this.handlePointerUp.bind(this));
            this.addEventListener('pointercancel', this.handlePointerCancel.bind(this));
            this.addEventListener('pointerleave', this.handlePointerLeave.bind(this));
            this.addEventListener('pointerenter', this.handlePointerEnter.bind(this));
            this.addEventListener('contextmenu', this.handleContextMenu.bind(this));
            this.addEventListener('keydown', this.handleKeyDown.bind(this));
            this.addEventListener('keyup', this.handleKeyUp.bind(this));
        }
        updated(changedProperties) {
            super.updated(changedProperties);
            if (changedProperties.has('tabIndex') && (this.tabIndex === -1 || this.tabIndex === null)) {
                this.ripple?.endPress();
            }
        }
        connectedCallback() {
            super.connectedCallback();
            if (!this.hasAttribute('role')) {
                this.setAttribute('role', 'listitem');
            }
        }
        willUpdate(cp) {
            if (cp.has('disabled')) {
                setDisabled(this, cp.get('disabled'));
            }
        }
        handleKeyDown(e) {
            if (this.disabled || this.static) {
                e.stopPropagation();
                e.preventDefault();
                return;
            }
            if (e.key !== ' ' && e.key !== 'Enter')
                return;
            // do not prevent default, so that parent elements can handle the key event
            this.beginPress({ positionEvent: e });
        }
        handleKeyUp(e) {
            if (this.disabled || this.static) {
                e.stopPropagation();
                e.preventDefault();
                return;
            }
            if (e.key !== ' ' && e.key !== 'Enter')
                return;
            // do not prevent default, so that parent elements can handle the key event
            this.endPress({ cancelled: false, actionData: { item: this } });
        }
        beginPress(options) {
            if (this.disabled || this.static) {
                return;
            }
            super.beginPress(options);
            this.ripple.beginPress(options.positionEvent);
        }
        endPress(options) {
            if (this.disabled || this.static) {
                return;
            }
            super.endPress(options);
            this.ripple.endPress();
        }
        handlePointerEnter(e) {
            if (this.disabled || this.static) {
                e.stopPropagation();
                e.preventDefault();
                return;
            }
            this.ripple.beginHover(e);
        }
        handlePointerLeave(e) {
            if (this.disabled || this.static) {
                e.stopPropagation();
                e.preventDefault();
                return;
            }
            super.handlePointerLeave(e);
            this.ripple.endHover();
        }
        /**
         * Focuses list item and makes list item focusable via keyboard.
         */
        activate() {
            if (this.disabled || this.static) {
                return;
            }
            this.setAttribute('tabindex', '0');
            this.focus();
        }
        /**
         * Returns true if list item is currently focused and is focusable.
         */
        isActive() {
            if (this.disabled || this.static) {
                return false;
            }
            return this.getAttribute('tabindex') === '0';
        }
        /**
         * Removes list item from sequential keyboard navigation.
         */
        deactivate() {
            if (this.disabled || this.static) {
                return;
            }
            this.removeAttribute('tabindex');
        }
        handleSlotChange() {
            this.requestUpdate();
        }
        render() {
            const { pressed = false } = this;
            const surfaceClasses = classMap({
                surface: true,
                pressed,
            });
            return html `
      <md-focus-ring part="focus-ring" .control="${this}" inward></md-focus-ring>
      <div class="${surfaceClasses}" ${ripple(() => this.ripple)}>
        <div class="container"></div>
        <div class="state"></div>
        <div class="content">${this.renderStart()} ${this.renderBody()} ${this.renderEnd()}</div>
        ${this.renderRipple()}
      </div>
    `;
        }
        renderRipple = (__runInitializers(this, _tabIndex_extraInitializers), () => {
            const { disabled, static: isStatic } = this;
            return html `<ui-ripple class="ripple" ?disabled="${disabled || isStatic}"></ui-ripple>`;
        });
        renderStart() {
            return html `<div class="start">
      <slot name="start" @slotchange=${this.handleSlotChange}></slot>
    </div>`;
        }
        renderEnd() {
            return html `<div class="end">
      <slot name="end" @slotchange=${this.handleSlotChange}></slot>
      <span class="trailing-supporting-text"><slot name="end-text"></slot></span>
    </div>`;
        }
        renderBody() {
            const { lines = ListItemLines.one } = this;
            const hasSupportingText = lines !== ListItemLines.one;
            return html `
      <div class="body">
        <span class="headline"><slot></slot></span>
        ${hasSupportingText ? html `<span class="supporting-text"><slot name="supporting-text"></slot></span>` : ''}
      </div>
    `;
        }
    };
})();
/**
 * @slot
 * @slot start
 * @slot end
 * @slot end-text
 * @slot supporting-text
 */
export default UiListItem;
//# sourceMappingURL=ListItem.js.map