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;
};
import { UmbDocumentItemRepository } from '../../../item/index.js';
import { UmbDocumentNotificationsRepository } from '../repository/document-notifications.repository.js';
import { css, customElement, html, repeat, state } from '../../../../../../external/lit/index.js';
import { UmbModalBaseElement } from '../../../../../core/modal/index.js';
import { UmbTextStyles } from '../../../../../core/style/index.js';
let UmbDocumentNotificationsModalElement = class UmbDocumentNotificationsModalElement extends UmbModalBaseElement {
    constructor() {
        super(...arguments);
        this.#documentNotificationsRepository = new UmbDocumentNotificationsRepository(this);
        this._settings = [];
        this._documentName = '';
    }
    #unique;
    #documentNotificationsRepository;
    firstUpdated() {
        this.#unique = this.data?.unique;
        this.#readNotificationSettings();
        this.#getDocumentName();
    }
    async #getDocumentName() {
        if (!this.#unique)
            return;
        // Should this be done here or in the action file?
        const { data } = await new UmbDocumentItemRepository(this).requestItems([this.#unique]);
        if (!data)
            return;
        const item = data[0];
        //TODO How do we ensure we get the correct variant?
        this._documentName = item.variants[0]?.name;
    }
    async #readNotificationSettings() {
        if (!this.#unique)
            return;
        const { data } = await this.#documentNotificationsRepository.readNotifications(this.#unique);
        if (!data)
            return;
        this._settings = data;
    }
    async #updateNotificationSettings() {
        if (!this.#unique)
            return;
        const subscribedActionIds = this._settings.filter((x) => x.subscribed).map((x) => x.actionId);
        const { error } = await this.#documentNotificationsRepository.updateNotifications(this.#unique, this._documentName, {
            subscribedActionIds,
        });
        if (error)
            return;
        this._submitModal();
    }
    async #updateSubscription(actionId) {
        this._settings = this._settings.map((setting) => {
            if (setting.actionId === actionId) {
                const subscribed = !setting.subscribed;
                return { ...setting, subscribed };
            }
            return setting;
        });
    }
    render() {
        return html `
			<umb-body-layout headline=${this.localize.term('notifications_notifications')}>
				<uui-box .headline=${this.localize.term('notifications_editNotifications', this._documentName)}>
					${repeat(this._settings, (setting) => setting.actionId, (setting) => {
            const localizationKey = `actions_${setting.alias}`;
            const localization = this.localize.termOrDefault(localizationKey, setting.alias);
            return html `<uui-toggle
								id=${setting.actionId}
								@change=${() => this.#updateSubscription(setting.actionId)}
								.label=${localization}
								?checked=${setting.subscribed}></uui-toggle>`;
        })}
				</uui-box>
				<umb-footer-layout slot="footer">
					<uui-button
						slot="actions"
						look="secondary"
						label=${this.localize.term('general_cancel')}
						@click=${this._rejectModal}></uui-button>
					<uui-button
						slot="actions"
						look="primary"
						color="positive"
						label=${this.localize.term('buttons_save')}
						@click=${this.#updateNotificationSettings}></uui-button>
				</umb-footer-layout>
			</umb-body-layout>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			uui-toggle {
				display: block;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbDocumentNotificationsModalElement.prototype, "_settings", void 0);
__decorate([
    state()
], UmbDocumentNotificationsModalElement.prototype, "_documentName", void 0);
UmbDocumentNotificationsModalElement = __decorate([
    customElement('umb-document-notifications-modal')
], UmbDocumentNotificationsModalElement);
export { UmbDocumentNotificationsModalElement };
export default UmbDocumentNotificationsModalElement;
