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, html, customElement, css, state, cssManager, } from '@design.estate/dees-element';
import * as appstate from '../../appstate.js';
import * as interfaces from '../../../dist_ts_interfaces/index.js';
import { viewHostCss } from '../shared/css.js';
import {} from '@design.estate/dees-catalog';
let OpsViewEmailDomains = (() => {
    let _classDecorators = [customElement('ops-view-email-domains')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = DeesElement;
    let _emailDomainsState_decorators;
    let _emailDomainsState_initializers = [];
    let _emailDomainsState_extraInitializers = [];
    let _domainsState_decorators;
    let _domainsState_initializers = [];
    let _domainsState_extraInitializers = [];
    var OpsViewEmailDomains = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _emailDomainsState_decorators = [state()];
            _domainsState_decorators = [state()];
            __esDecorate(this, null, _emailDomainsState_decorators, { kind: "accessor", name: "emailDomainsState", static: false, private: false, access: { has: obj => "emailDomainsState" in obj, get: obj => obj.emailDomainsState, set: (obj, value) => { obj.emailDomainsState = value; } }, metadata: _metadata }, _emailDomainsState_initializers, _emailDomainsState_extraInitializers);
            __esDecorate(this, null, _domainsState_decorators, { kind: "accessor", name: "domainsState", static: false, private: false, access: { has: obj => "domainsState" in obj, get: obj => obj.domainsState, set: (obj, value) => { obj.domainsState = value; } }, metadata: _metadata }, _domainsState_initializers, _domainsState_extraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            OpsViewEmailDomains = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #emailDomainsState_accessor_storage = __runInitializers(this, _emailDomainsState_initializers, appstate.emailDomainsStatePart.getState());
        get emailDomainsState() { return this.#emailDomainsState_accessor_storage; }
        set emailDomainsState(value) { this.#emailDomainsState_accessor_storage = value; }
        #domainsState_accessor_storage = (__runInitializers(this, _emailDomainsState_extraInitializers), __runInitializers(this, _domainsState_initializers, appstate.domainsStatePart.getState()));
        get domainsState() { return this.#domainsState_accessor_storage; }
        set domainsState(value) { this.#domainsState_accessor_storage = value; }
        constructor() {
            super();
            __runInitializers(this, _domainsState_extraInitializers);
            const sub = appstate.emailDomainsStatePart.select().subscribe((s) => {
                this.emailDomainsState = s;
            });
            this.rxSubscriptions.push(sub);
            const domSub = appstate.domainsStatePart.select().subscribe((s) => {
                this.domainsState = s;
            });
            this.rxSubscriptions.push(domSub);
        }
        async connectedCallback() {
            await super.connectedCallback();
            await appstate.emailDomainsStatePart.dispatchAction(appstate.fetchEmailDomainsAction, null);
            await appstate.domainsStatePart.dispatchAction(appstate.fetchDomainsAndProvidersAction, null);
        }
        static styles = [
            cssManager.defaultStyles,
            viewHostCss,
            css `
      .emailDomainsContainer {
        display: flex;
        flex-direction: column;
        gap: 24px;
      }

      .statusBadge {
        display: inline-flex;
        align-items: center;
        padding: 3px 10px;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 600;
        letter-spacing: 0.02em;
        text-transform: uppercase;
      }

      .statusBadge.valid {
        background: ${cssManager.bdTheme('#dcfce7', '#14532d')};
        color: ${cssManager.bdTheme('#166534', '#4ade80')};
      }

      .statusBadge.missing {
        background: ${cssManager.bdTheme('#fef2f2', '#450a0a')};
        color: ${cssManager.bdTheme('#991b1b', '#f87171')};
      }

      .statusBadge.invalid {
        background: ${cssManager.bdTheme('#fff7ed', '#431407')};
        color: ${cssManager.bdTheme('#9a3412', '#fb923c')};
      }

      .statusBadge.unchecked {
        background: ${cssManager.bdTheme('#f3f4f6', '#1f2937')};
        color: ${cssManager.bdTheme('#4b5563', '#9ca3af')};
      }

      .sourceBadge {
        display: inline-flex;
        align-items: center;
        padding: 3px 8px;
        border-radius: 4px;
        font-size: 11px;
        font-weight: 500;
        background: ${cssManager.bdTheme('#f3f4f6', '#1f2937')};
        color: ${cssManager.bdTheme('#374151', '#d1d5db')};
      }
    `,
        ];
        render() {
            const domains = this.emailDomainsState.domains;
            const settings = this.emailDomainsState.settings;
            const validCount = domains.filter((d) => d.dnsStatus.mx === 'valid' &&
                d.dnsStatus.spf === 'valid' &&
                d.dnsStatus.dkim === 'valid' &&
                d.dnsStatus.dmarc === 'valid').length;
            const issueCount = domains.length - validCount;
            const tiles = [
                {
                    id: 'total',
                    title: 'Total Domains',
                    value: domains.length,
                    type: 'number',
                    icon: 'lucide:globe',
                    color: '#3b82f6',
                },
                {
                    id: 'valid',
                    title: 'Valid DNS',
                    value: validCount,
                    type: 'number',
                    icon: 'lucide:Check',
                    color: '#22c55e',
                },
                {
                    id: 'server',
                    title: 'Server',
                    value: settings?.enabled ? 'enabled' : 'disabled',
                    type: 'text',
                    icon: 'lucide:mail-check',
                    color: settings?.enabled ? '#22c55e' : '#6b7280',
                },
                {
                    id: 'ports',
                    title: 'SMTP Ports',
                    value: settings?.ports?.join(', ') || 'none',
                    type: 'text',
                    icon: 'lucide:plug',
                    color: '#0ea5e9',
                },
                {
                    id: 'issues',
                    title: 'Issues',
                    value: issueCount,
                    type: 'number',
                    icon: 'lucide:TriangleAlert',
                    color: issueCount > 0 ? '#ef4444' : '#22c55e',
                },
                {
                    id: 'dkim',
                    title: 'DKIM Active',
                    value: domains.filter((d) => d.dkim.publicKey).length,
                    type: 'number',
                    icon: 'lucide:KeyRound',
                    color: '#8b5cf6',
                },
            ];
            return html `
      <dees-heading level="3">Email Domains</dees-heading>

      <div class="emailDomainsContainer">
        <dees-statsgrid
          .tiles=${tiles}
          .minTileWidth=${200}
          .gridActions=${[
                {
                    name: 'Refresh',
                    iconName: 'lucide:RefreshCw',
                    action: async () => {
                        await appstate.emailDomainsStatePart.dispatchAction(appstate.fetchEmailDomainsAction, null);
                    },
                },
                {
                    name: 'Settings',
                    iconName: 'lucide:settings',
                    action: async () => {
                        await this.showSettingsDialog();
                    },
                },
            ]}
        ></dees-statsgrid>

        <dees-table
          .heading1=${'Email Domains'}
          .heading2=${'DKIM, SPF, DMARC and MX management'}
          .data=${domains}
          .showColumnFilters=${true}
          .displayFunction=${(d) => ({
                Domain: d.domain,
                Source: this.renderSourceBadge(d.linkedDomainId),
                MX: this.renderDnsStatus(d.dnsStatus.mx),
                SPF: this.renderDnsStatus(d.dnsStatus.spf),
                DKIM: this.renderDnsStatus(d.dnsStatus.dkim),
                DMARC: this.renderDnsStatus(d.dnsStatus.dmarc),
            })}
          .dataActions=${[
                {
                    name: 'Add Email Domain',
                    iconName: 'lucide:plus',
                    type: ['header'],
                    actionFunc: async () => {
                        await this.showCreateDialog();
                    },
                },
                {
                    name: 'Validate DNS',
                    iconName: 'lucide:search-check',
                    type: ['inRow', 'contextmenu'],
                    actionFunc: async (actionData) => {
                        const d = actionData.item;
                        await appstate.emailDomainsStatePart.dispatchAction(appstate.validateEmailDomainAction, d.id);
                        const { DeesToast } = await import('@design.estate/dees-catalog');
                        DeesToast.show({ message: `DNS validated for ${d.domain}`, type: 'success', duration: 2500 });
                    },
                },
                {
                    name: 'Provision DNS',
                    iconName: 'lucide:wand-sparkles',
                    type: ['inRow', 'contextmenu'],
                    actionFunc: async (actionData) => {
                        const d = actionData.item;
                        await appstate.emailDomainsStatePart.dispatchAction(appstate.provisionEmailDomainDnsAction, d.id);
                        const { DeesToast } = await import('@design.estate/dees-catalog');
                        DeesToast.show({ message: `DNS records provisioned for ${d.domain}`, type: 'success', duration: 2500 });
                    },
                },
                {
                    name: 'View DNS Records',
                    iconName: 'lucide:list',
                    type: ['inRow', 'contextmenu'],
                    actionFunc: async (actionData) => {
                        const d = actionData.item;
                        await this.showDnsRecordsDialog(d);
                    },
                },
                {
                    name: 'Delete',
                    iconName: 'lucide:trash2',
                    type: ['inRow', 'contextmenu'],
                    actionFunc: async (actionData) => {
                        const d = actionData.item;
                        await appstate.emailDomainsStatePart.dispatchAction(appstate.deleteEmailDomainAction, d.id);
                    },
                },
            ]}
          dataName="email domain"
        ></dees-table>
      </div>
    `;
        }
        renderDnsStatus(status) {
            return html `<span class="statusBadge ${status}">${status}</span>`;
        }
        renderSourceBadge(linkedDomainId) {
            const domain = this.domainsState.domains.find((d) => d.id === linkedDomainId);
            if (!domain)
                return html `<span class="sourceBadge">unknown</span>`;
            const label = domain.source === 'dcrouter'
                ? 'dcrouter'
                : this.domainsState.providers.find((p) => p.id === domain.providerId)?.name || 'provider';
            return html `<span class="sourceBadge">${label}</span>`;
        }
        parsePortList(value) {
            return value
                .split(',')
                .map((part) => Number.parseInt(part.trim(), 10))
                .filter((port) => Number.isInteger(port));
        }
        parsePortMapping(value) {
            const trimmed = value.trim();
            if (!trimmed)
                return null;
            const mapping = {};
            for (const pair of trimmed.split(',')) {
                const [externalPort, internalPort] = pair
                    .split(':')
                    .map((part) => Number.parseInt(part.trim(), 10));
                if (Number.isInteger(externalPort) && Number.isInteger(internalPort)) {
                    mapping[externalPort] = internalPort;
                }
            }
            return Object.keys(mapping).length > 0 ? mapping : null;
        }
        formatPortMapping(mapping) {
            if (!mapping)
                return '';
            return Object.entries(mapping)
                .map(([externalPort, internalPort]) => `${externalPort}:${internalPort}`)
                .join(', ');
        }
        async showSettingsDialog() {
            const { DeesModal, DeesToast } = await import('@design.estate/dees-catalog');
            const settings = this.emailDomainsState.settings;
            DeesModal.createAndShow({
                heading: 'Email Server Settings',
                content: html `
        <dees-form>
          <dees-input-checkbox
            .key=${'enabled'}
            .label=${'Enable email server'}
            .value=${settings?.enabled ?? false}
          ></dees-input-checkbox>
          <dees-input-text
            .key=${'hostname'}
            .label=${'SMTP hostname'}
            .description=${'Public hostname used in SMTP banners and DNS records'}
            .value=${settings?.hostname || ''}
          ></dees-input-text>
          <dees-input-text
            .key=${'ports'}
            .label=${'Public ports'}
            .description=${'Comma-separated SMTP ingress ports, e.g. 25, 587, 465'}
            .value=${settings?.ports?.join(', ') || '25, 587, 465'}
          ></dees-input-text>
          <dees-input-text
            .key=${'portMapping'}
            .label=${'Port mapping'}
            .description=${'Optional external:internal pairs, e.g. 25:10025, 587:10587'}
            .value=${this.formatPortMapping(settings?.portMapping)}
          ></dees-input-text>
          <dees-input-text
            .key=${'maxMessageSize'}
            .label=${'Max message size'}
            .description=${'Bytes; leave empty for smartmta default'}
            .value=${settings?.maxMessageSize ? String(settings.maxMessageSize) : ''}
          ></dees-input-text>
          <dees-input-text
            .key=${'receivedEmailsPath'}
            .label=${'Received emails path'}
            .description=${'Optional storage path for received email artifacts'}
            .value=${settings?.receivedEmailsPath || ''}
          ></dees-input-text>
        </dees-form>
      `,
                menuOptions: [
                    { name: 'Cancel', action: async (m) => m.destroy() },
                    {
                        name: 'Save',
                        action: async (m) => {
                            const form = m.shadowRoot?.querySelector('.content')?.querySelector('dees-form');
                            if (!form)
                                return;
                            const data = await form.collectFormData();
                            const maxMessageSizeRaw = String(data.maxMessageSize || '').trim();
                            await appstate.emailDomainsStatePart.dispatchAction(appstate.updateEmailServerSettingsAction, {
                                enabled: Boolean(data.enabled),
                                hostname: String(data.hostname || '').trim() || null,
                                ports: this.parsePortList(String(data.ports || '')),
                                portMapping: this.parsePortMapping(String(data.portMapping || '')),
                                maxMessageSize: maxMessageSizeRaw ? Number.parseInt(maxMessageSizeRaw, 10) : null,
                                receivedEmailsPath: String(data.receivedEmailsPath || '').trim() || null,
                            });
                            DeesToast.show({ message: 'Email settings saved', type: 'success', duration: 2500 });
                            m.destroy();
                        },
                    },
                ],
            });
        }
        async showCreateDialog() {
            const { DeesModal } = await import('@design.estate/dees-catalog');
            const domainOptions = this.domainsState.domains.map((d) => ({
                option: `${d.name} (${d.source})`,
                key: d.id,
            }));
            DeesModal.createAndShow({
                heading: 'Add Email Domain',
                content: html `
        <dees-form>
          <dees-input-dropdown
            .key=${'linkedDomainId'}
            .label=${'Domain'}
            .description=${'Select an existing DNS domain'}
            .options=${domainOptions}
            .required=${true}
          ></dees-input-dropdown>
          <dees-input-text
            .key=${'subdomain'}
            .label=${'Subdomain'}
            .description=${'Leave empty for bare domain, e.g. "mail" for mail.example.com'}
          ></dees-input-text>
          <dees-input-text
            .key=${'dkimSelector'}
            .label=${'DKIM Selector'}
            .description=${'Identifier used in DNS record name'}
            .value=${'default'}
          ></dees-input-text>
          <dees-input-dropdown
            .key=${'dkimKeySize'}
            .label=${'DKIM Key Size'}
            .options=${[
                    { option: '2048 (recommended)', key: '2048' },
                    { option: '1024', key: '1024' },
                    { option: '4096', key: '4096' },
                ]}
            .selectedOption=${{ option: '2048 (recommended)', key: '2048' }}
          ></dees-input-dropdown>
          <dees-input-checkbox
            .key=${'rotateKeys'}
            .label=${'Auto-rotate DKIM keys'}
            .value=${false}
          ></dees-input-checkbox>
        </dees-form>
      `,
                menuOptions: [
                    { name: 'Cancel', action: async (m) => m.destroy() },
                    {
                        name: 'Create',
                        action: async (m) => {
                            const form = m.shadowRoot?.querySelector('.content')?.querySelector('dees-form');
                            if (!form)
                                return;
                            const data = await form.collectFormData();
                            const linkedDomainId = typeof data.linkedDomainId === 'object'
                                ? data.linkedDomainId.key
                                : data.linkedDomainId;
                            const keySize = typeof data.dkimKeySize === 'object'
                                ? parseInt(data.dkimKeySize.key, 10)
                                : parseInt(data.dkimKeySize || '2048', 10);
                            const subdomain = data.subdomain?.trim() || undefined;
                            await appstate.emailDomainsStatePart.dispatchAction(appstate.createEmailDomainAction, {
                                linkedDomainId,
                                subdomain,
                                dkimSelector: data.dkimSelector || 'default',
                                dkimKeySize: keySize,
                                rotateKeys: Boolean(data.rotateKeys),
                            });
                            m.destroy();
                        },
                    },
                ],
            });
        }
        async showDnsRecordsDialog(emailDomain) {
            const { DeesModal, DeesToast } = await import('@design.estate/dees-catalog');
            // Fetch required DNS records
            let records = [];
            try {
                const response = await appstate.fetchEmailDomainDnsRecords(emailDomain.id);
                records = response.records;
            }
            catch {
                records = [];
            }
            DeesModal.createAndShow({
                heading: `DNS Records: ${emailDomain.domain}`,
                content: html `
        <dees-table
          .data=${records}
          .displayFunction=${(r) => ({
                    Type: r.type,
                    Name: r.name,
                    Value: r.value,
                    Status: html `<span class="statusBadge ${r.status}">${r.status}</span>`,
                })}
          .dataActions=${[
                    {
                        name: 'Copy Value',
                        iconName: 'lucide:copy',
                        type: ['inRow'],
                        actionFunc: async (actionData) => {
                            const rec = actionData.item;
                            await navigator.clipboard.writeText(rec.value);
                            DeesToast.show({ message: 'Copied to clipboard', type: 'success', duration: 1500 });
                        },
                    },
                ]}
          dataName="DNS record"
        ></dees-table>
      `,
                menuOptions: [
                    {
                        name: 'Auto-Provision All',
                        action: async (m) => {
                            await appstate.emailDomainsStatePart.dispatchAction(appstate.provisionEmailDomainDnsAction, emailDomain.id);
                            DeesToast.show({ message: 'DNS records provisioned', type: 'success', duration: 2500 });
                            m.destroy();
                        },
                    },
                    { name: 'Close', action: async (m) => m.destroy() },
                ],
            });
        }
        static {
            __runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return OpsViewEmailDomains = _classThis;
})();
export { OpsViewEmailDomains };
//# sourceMappingURL=data:application/json;base64, 