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 `
      <ops-sectionheading>Email Operations</ops-sectionheading>
      <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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3BzLXZpZXctZW1haWxzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vdHNfd2ViL2VsZW1lbnRzL29wcy12aWV3LWVtYWlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxRQUFRLEVBQUUsSUFBSSxFQUFFLGFBQWEsRUFBdUIsR0FBRyxFQUFFLEtBQUssRUFBRSxVQUFVLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUN0SSxPQUFPLEtBQUssT0FBTyxNQUFNLGVBQWUsQ0FBQztBQUN6QyxPQUFPLEtBQUssUUFBUSxNQUFNLGdCQUFnQixDQUFDO0FBQzNDLE9BQU8sS0FBSyxNQUFNLE1BQU0sbUJBQW1CLENBQUM7QUFDNUMsT0FBTyxLQUFLLFVBQVUsTUFBTSxtQ0FBbUMsQ0FBQztJQVNuRCxhQUFhOzRCQUR6QixhQUFhLENBQUMsaUJBQWlCLENBQUM7Ozs7c0JBQ0UsV0FBVzs7Ozs7Ozs7Ozs7Ozs2QkFBbkIsU0FBUSxXQUFXOzs7O2tDQUMzQyxLQUFLLEVBQUU7eUNBR1AsS0FBSyxFQUFFO3VDQUdQLEtBQUss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