var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for (var i = decorators.length - 1; i >= 0; i--) {
        var context = {};
        for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
        for (var p in contextIn.access) context.access[p] = contextIn.access[p];
        context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
        var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        }
        else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for (var i = 0; i < initializers.length; i++) {
        value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    }
    return useValue ? value : void 0;
};
import { DeesElement, property, html, customElement, css, state, cssManager } from '@design.estate/dees-element';
import * as plugins from '../../plugins.js';
import * as appstate from '../../appstate.js';
import * as shared from '../shared/index.js';
import * as interfaces from '../../../dist_ts_interfaces/index.js';
let OpsViewEmails = (() => {
    let _classDecorators = [customElement('ops-view-emails')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = DeesElement;
    let _emails_decorators;
    let _emails_initializers = [];
    let _emails_extraInitializers = [];
    let _selectedEmail_decorators;
    let _selectedEmail_initializers = [];
    let _selectedEmail_extraInitializers = [];
    let _currentView_decorators;
    let _currentView_initializers = [];
    let _currentView_extraInitializers = [];
    let _isLoading_decorators;
    let _isLoading_initializers = [];
    let _isLoading_extraInitializers = [];
    var OpsViewEmails = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _emails_decorators = [state()];
            _selectedEmail_decorators = [state()];
            _currentView_decorators = [state()];
            _isLoading_decorators = [state()];
            __esDecorate(this, null, _emails_decorators, { kind: "accessor", name: "emails", static: false, private: false, access: { has: obj => "emails" in obj, get: obj => obj.emails, set: (obj, value) => { obj.emails = value; } }, metadata: _metadata }, _emails_initializers, _emails_extraInitializers);
            __esDecorate(this, null, _selectedEmail_decorators, { kind: "accessor", name: "selectedEmail", static: false, private: false, access: { has: obj => "selectedEmail" in obj, get: obj => obj.selectedEmail, set: (obj, value) => { obj.selectedEmail = value; } }, metadata: _metadata }, _selectedEmail_initializers, _selectedEmail_extraInitializers);
            __esDecorate(this, null, _currentView_decorators, { kind: "accessor", name: "currentView", static: false, private: false, access: { has: obj => "currentView" in obj, get: obj => obj.currentView, set: (obj, value) => { obj.currentView = value; } }, metadata: _metadata }, _currentView_initializers, _currentView_extraInitializers);
            __esDecorate(this, null, _isLoading_decorators, { kind: "accessor", name: "isLoading", static: false, private: false, access: { has: obj => "isLoading" in obj, get: obj => obj.isLoading, set: (obj, value) => { obj.isLoading = value; } }, metadata: _metadata }, _isLoading_initializers, _isLoading_extraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            OpsViewEmails = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #emails_accessor_storage = __runInitializers(this, _emails_initializers, []);
        get emails() { return this.#emails_accessor_storage; }
        set emails(value) { this.#emails_accessor_storage = value; }
        #selectedEmail_accessor_storage = (__runInitializers(this, _emails_extraInitializers), __runInitializers(this, _selectedEmail_initializers, null));
        get selectedEmail() { return this.#selectedEmail_accessor_storage; }
        set selectedEmail(value) { this.#selectedEmail_accessor_storage = value; }
        #currentView_accessor_storage = (__runInitializers(this, _selectedEmail_extraInitializers), __runInitializers(this, _currentView_initializers, 'list'));
        get currentView() { return this.#currentView_accessor_storage; }
        set currentView(value) { this.#currentView_accessor_storage = value; }
        #isLoading_accessor_storage = (__runInitializers(this, _currentView_extraInitializers), __runInitializers(this, _isLoading_initializers, false));
        get isLoading() { return this.#isLoading_accessor_storage; }
        set isLoading(value) { this.#isLoading_accessor_storage = value; }
        stateSubscription = __runInitializers(this, _isLoading_extraInitializers);
        async connectedCallback() {
            await super.connectedCallback();
            this.stateSubscription = appstate.emailOpsStatePart.select().subscribe((state) => {
                this.emails = state.emails;
                this.isLoading = state.isLoading;
            });
            // Initial fetch
            await appstate.emailOpsStatePart.dispatchAction(appstate.fetchAllEmailsAction, null);
        }
        async disconnectedCallback() {
            await super.disconnectedCallback();
            if (this.stateSubscription) {
                this.stateSubscription.unsubscribe();
            }
        }
        static styles = [
            cssManager.defaultStyles,
            shared.viewHostCss,
            css `
      :host {
        display: block;
        height: 100%;
      }

      .viewContainer {
        height: 100%;
      }
    `,
        ];
        render() {
            return html `
      <dees-heading level="3">Email Log</dees-heading>
      <div class="viewContainer">
        ${this.currentView === 'detail' && this.selectedEmail
                ? html `
              <sz-mta-detail-view
                .email=${this.selectedEmail}
                @back=${this.handleBack}
              ></sz-mta-detail-view>
            `
                : html `
              <sz-mta-list-view
                .emails=${this.emails}
                @email-click=${this.handleEmailClick}
              ></sz-mta-list-view>
            `}
      </div>
    `;
        }
        async handleEmailClick(e) {
            const emailSummary = e.detail;
            try {
                const context = appstate.loginStatePart.getState();
                const request = new plugins.domtools.plugins.typedrequest.TypedRequest('/typedrequest', 'getEmailDetail');
                const response = await request.fire({
                    identity: context.identity,
                    emailId: emailSummary.id,
                });
                if (response.email) {
                    this.selectedEmail = response.email;
                    this.currentView = 'detail';
                }
            }
            catch (error) {
                console.error('Failed to fetch email detail:', error);
            }
        }
        handleBack() {
            this.selectedEmail = null;
            this.currentView = 'list';
        }
        static {
            __runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return OpsViewEmails = _classThis;
})();
export { OpsViewEmails };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3BzLXZpZXctZW1haWxzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vdHNfd2ViL2VsZW1lbnRzL2VtYWlsL29wcy12aWV3LWVtYWlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxRQUFRLEVBQUUsSUFBSSxFQUFFLGFBQWEsRUFBdUIsR0FBRyxFQUFFLEtBQUssRUFBRSxVQUFVLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUN0SSxPQUFPLEtBQUssT0FBTyxNQUFNLGtCQUFrQixDQUFDO0FBQzVDLE9BQU8sS0FBSyxRQUFRLE1BQU0sbUJBQW1CLENBQUM7QUFDOUMsT0FBTyxLQUFLLE1BQU0sTUFBTSxvQkFBb0IsQ0FBQztBQUM3QyxPQUFPLEtBQUssVUFBVSxNQUFNLHNDQUFzQyxDQUFDO0lBU3RELGFBQWE7NEJBRHpCLGFBQWEsQ0FBQyxpQkFBaUIsQ0FBQzs7OztzQkFDRSxXQUFXOzs7Ozs7Ozs7Ozs7OzZCQUFuQixTQUFRLFdBQVc7Ozs7a0NBQzNDLEtBQUssRUFBRTt5Q0FHUCxLQUFLLEVBQUU7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