// #region Imports
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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Listen } from '../../../../Events/Decorators/ListenDecorator';
import { CssAspectRatio } from '@breadstone/mosaik-themes';
import { Dimensionable } from '../../../Behaviors/Dimensionable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Themeable } from '../../../Behaviors/Themeable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { audioElementTemplate } from './AudioElementTemplate';
import { audioElementJoyStyle } from './Themes/AudioElement.Joy';
import { audioElementRetroStyle } from './Themes/AudioElement.Retro';
import { audioElementCosmopolitanStyle } from './Themes/AudioElement.Cosmopolitan';
import { AudioLegendPosition } from '../../../Types/AudioLegendPosition';
// #endregion
/**
 * Audio - A media control for playing sound.
 *
 * @element mosaik-audio
 *
 * @slot - The default content slot.
 * @slot legend - The legend slot.
 *
 * @csspart audio - The native audio element part.
 * @csspart legend - The legend part.
 * @csspart content - The content overlay part.
 *
 * @public
 */
let AudioElement = class AudioElement extends Themeable(Disableable(Dimensionable(CustomElement))) {
    // #region Fields
    _audioElement;
    _src;
    _autoPlay;
    _showControls;
    _legend;
    _legendPosition;
    _ratio;
    _muted;
    _loop;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._src = null;
        this._autoPlay = false;
        this._showControls = true;
        this._legend = '';
        this._legendPosition = AudioLegendPosition.None;
        this._ratio = 'auto';
        this._muted = false;
        this._loop = false;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the {@link is} property.
     * The natural name of this element.
     */
    static get is() {
        return 'mosaik-audio';
    }
    /**
     * Gets or sets the `src` property.
     */
    get src() {
        return this._src;
    }
    set src(value) {
        if (this._src !== value) {
            this._src = value;
            this.requestUpdate('src');
        }
    }
    /**
     * Gets or sets the `autoPlay` property.
     */
    get autoPlay() {
        return this._autoPlay;
    }
    set autoPlay(value) {
        if (this._autoPlay !== value) {
            this._autoPlay = value;
            this.requestUpdate('autoPlay');
        }
    }
    /**
     * Gets or sets the `showControls` property.
     */
    get showControls() {
        return this._showControls;
    }
    set showControls(value) {
        if (this._showControls !== value) {
            this._showControls = value;
            this.requestUpdate('showControls');
        }
    }
    /**
     * Gets or sets the `legend` property.
     */
    get legend() {
        return this._legend;
    }
    set legend(value) {
        if (this._legend !== value) {
            this._legend = value;
            this.requestUpdate('legend');
        }
    }
    /**
     * Gets or sets the `legendPosition` property.
     */
    get legendPosition() {
        return this._legendPosition;
    }
    set legendPosition(value) {
        if (this._legendPosition !== value) {
            this._legendPosition = value;
            this.requestUpdate('legendPosition');
        }
    }
    /**
     * Gets or sets the `ratio` property.
     */
    get ratio() {
        return this._ratio;
    }
    set ratio(value) {
        if (this._ratio !== value) {
            this._ratio = value;
            this.requestUpdate('ratio');
        }
    }
    /**
     * Returns the `muted` property.
     */
    get muted() {
        return this._muted;
    }
    set muted(value) {
        if (this._muted !== value) {
            this._muted = value;
            this.requestUpdate('muted');
        }
    }
    /**
     * Returns the `loop` property.
     */
    get loop() {
        return this._loop;
    }
    set loop(value) {
        if (this._loop !== value) {
            this._loop = value;
            this.requestUpdate('loop');
        }
    }
    /**
     * Returns true when a source is set.
     */
    get hasSrc() {
        return !!(this._audioElement.src || this._audioElement.srcObject);
    }
    // #endregion
    // #region Methods
    /**
     * Plays the audio.
     */
    play() {
        return this._audioElement.play();
    }
    /**
     * Pauses the audio.
     */
    pause() {
        this._audioElement.pause();
    }
    /**
     * Toggles playback.
     */
    toggle() {
        const isPlaying = this._audioElement.currentTime > 0 &&
            !this._audioElement.paused &&
            !this._audioElement.ended &&
            this._audioElement.readyState > 2;
        if (isPlaying) {
            this.pause();
        }
        else {
            void this.play();
        }
    }
    /**
     * Reloads the audio source.
     */
    load() {
        this._audioElement.load();
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this._audioElement = this.getTemplatePart('audio');
    }
    /**
     * Pauses when disabled.
     */
    onDisabledPropertyChanged(_prevValue, nextValue) {
        if (nextValue) {
            this._audioElement.pause();
        }
    }
    onClick() {
        if (!this.showControls) {
            this.toggle();
        }
    }
};
__decorate([
    Property({ type: Object }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], AudioElement.prototype, "src", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], AudioElement.prototype, "autoPlay", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], AudioElement.prototype, "showControls", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], AudioElement.prototype, "legend", null);
__decorate([
    Attribute({ type: AudioLegendPosition }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], AudioElement.prototype, "legendPosition", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], AudioElement.prototype, "ratio", null);
__decorate([
    Attribute({ type: Boolean }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], AudioElement.prototype, "muted", null);
__decorate([
    Attribute({ type: Boolean }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], AudioElement.prototype, "loop", null);
__decorate([
    Watch('disabled', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], AudioElement.prototype, "onDisabledPropertyChanged", null);
__decorate([
    Listen('click'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], AudioElement.prototype, "onClick", null);
AudioElement = __decorate([
    Component({
        selector: 'mosaik-audio',
        template: audioElementTemplate,
        themes: {
            joy: audioElementJoyStyle,
            retro: audioElementRetroStyle,
            cosmopolitan: audioElementCosmopolitanStyle
        },
        imports: [
            TextElement
        ]
    }),
    __metadata("design:paramtypes", [])
], AudioElement);
export { AudioElement };
//# sourceMappingURL=AudioElement.js.map