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 OpsViewNetworkTargets = (() => {
    let _classDecorators = [customElement('ops-view-networktargets')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = DeesElement;
    let _profilesState_decorators;
    let _profilesState_initializers = [];
    let _profilesState_extraInitializers = [];
    var OpsViewNetworkTargets = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _profilesState_decorators = [state()];
            __esDecorate(this, null, _profilesState_decorators, { kind: "accessor", name: "profilesState", static: false, private: false, access: { has: obj => "profilesState" in obj, get: obj => obj.profilesState, set: (obj, value) => { obj.profilesState = value; } }, metadata: _metadata }, _profilesState_initializers, _profilesState_extraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            OpsViewNetworkTargets = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #profilesState_accessor_storage = __runInitializers(this, _profilesState_initializers, appstate.profilesTargetsStatePart.getState());
        get profilesState() { return this.#profilesState_accessor_storage; }
        set profilesState(value) { this.#profilesState_accessor_storage = value; }
        constructor() {
            super();
            __runInitializers(this, _profilesState_extraInitializers);
            const sub = appstate.profilesTargetsStatePart.select().subscribe((newState) => {
                this.profilesState = newState;
            });
            this.rxSubscriptions.push(sub);
        }
        async connectedCallback() {
            await super.connectedCallback();
            await appstate.profilesTargetsStatePart.dispatchAction(appstate.fetchProfilesAndTargetsAction, null);
        }
        static styles = [
            cssManager.defaultStyles,
            viewHostCss,
            css `
      .targetsContainer {
        display: flex;
        flex-direction: column;
        gap: 24px;
      }
    `,
        ];
        render() {
            const targets = this.profilesState.targets;
            const statsTiles = [
                {
                    id: 'totalTargets',
                    title: 'Total Targets',
                    type: 'number',
                    value: targets.length,
                    icon: 'lucide:server',
                    description: 'Reusable network targets',
                    color: '#8b5cf6',
                },
            ];
            return html `
      <dees-heading level="3">Network Targets</dees-heading>
      <div class="targetsContainer">
        <dees-statsgrid .tiles=${statsTiles}></dees-statsgrid>
        <dees-table
          .heading1=${'Network Targets'}
          .heading2=${'Reusable host:port destinations for routes'}
          .data=${targets}
          .showColumnFilters=${true}
          .displayFunction=${(target) => ({
                Name: target.name,
                Host: Array.isArray(target.host) ? target.host.join(', ') : target.host,
                Port: target.port,
                Description: target.description || '-',
            })}
          .dataActions=${[
                {
                    name: 'Create Target',
                    iconName: 'lucide:plus',
                    type: ['header'],
                    actionFunc: async () => {
                        await this.showCreateTargetDialog();
                    },
                },
                {
                    name: 'Refresh',
                    iconName: 'lucide:rotateCw',
                    type: ['header'],
                    actionFunc: async () => {
                        await appstate.profilesTargetsStatePart.dispatchAction(appstate.fetchProfilesAndTargetsAction, null);
                    },
                },
                {
                    name: 'Edit',
                    iconName: 'lucide:pencil',
                    type: ['inRow', 'contextmenu'],
                    actionFunc: async (actionData) => {
                        const target = actionData.item;
                        await this.showEditTargetDialog(target);
                    },
                },
                {
                    name: 'Delete',
                    iconName: 'lucide:trash2',
                    type: ['inRow', 'contextmenu'],
                    actionFunc: async (actionData) => {
                        const target = actionData.item;
                        await this.deleteTarget(target);
                    },
                },
            ]}
        ></dees-table>
      </div>
    `;
        }
        async showCreateTargetDialog() {
            const { DeesModal } = await import('@design.estate/dees-catalog');
            DeesModal.createAndShow({
                heading: 'Create Network Target',
                content: html `
        <dees-form>
          <dees-input-text .key=${'name'} .label=${'Name'} .required=${true}></dees-input-text>
          <dees-input-text .key=${'host'} .label=${'Host'} .required=${true}></dees-input-text>
          <dees-input-text .key=${'port'} .label=${'Port'} .required=${true} .value=${'443'}></dees-input-text>
          <dees-input-text .key=${'description'} .label=${'Description'}></dees-input-text>
        </dees-form>
      `,
                menuOptions: [
                    { name: 'Cancel', action: async (modalArg) => modalArg.destroy() },
                    {
                        name: 'Create',
                        action: async (modalArg) => {
                            const form = modalArg.shadowRoot?.querySelector('.content')?.querySelector('dees-form');
                            if (!form)
                                return;
                            const data = await form.collectFormData();
                            await appstate.profilesTargetsStatePart.dispatchAction(appstate.createTargetAction, {
                                name: String(data.name),
                                description: data.description ? String(data.description) : undefined,
                                host: String(data.host),
                                port: parseInt(String(data.port)) || 443,
                            });
                            modalArg.destroy();
                        },
                    },
                ],
            });
        }
        async showEditTargetDialog(target) {
            const hostStr = Array.isArray(target.host) ? target.host.join(', ') : target.host;
            const { DeesModal } = await import('@design.estate/dees-catalog');
            DeesModal.createAndShow({
                heading: `Edit Target: ${target.name}`,
                content: html `
        <dees-form>
          <dees-input-text .key=${'name'} .label=${'Name'} .value=${target.name}></dees-input-text>
          <dees-input-text .key=${'host'} .label=${'Host'} .value=${hostStr}></dees-input-text>
          <dees-input-text .key=${'port'} .label=${'Port'} .value=${String(target.port)}></dees-input-text>
          <dees-input-text .key=${'description'} .label=${'Description'} .value=${target.description || ''}></dees-input-text>
        </dees-form>
      `,
                menuOptions: [
                    { name: 'Cancel', action: async (modalArg) => modalArg.destroy() },
                    {
                        name: 'Save',
                        action: async (modalArg) => {
                            const form = modalArg.shadowRoot?.querySelector('.content')?.querySelector('dees-form');
                            if (!form)
                                return;
                            const data = await form.collectFormData();
                            await appstate.profilesTargetsStatePart.dispatchAction(appstate.updateTargetAction, {
                                id: target.id,
                                name: String(data.name),
                                description: data.description ? String(data.description) : undefined,
                                host: String(data.host),
                                port: parseInt(String(data.port)) || 443,
                            });
                            modalArg.destroy();
                        },
                    },
                ],
            });
        }
        async deleteTarget(target) {
            await appstate.profilesTargetsStatePart.dispatchAction(appstate.deleteTargetAction, {
                id: target.id,
                force: false,
            });
            const currentState = appstate.profilesTargetsStatePart.getState();
            if (currentState.error?.includes('in use')) {
                const { DeesModal } = await import('@design.estate/dees-catalog');
                DeesModal.createAndShow({
                    heading: 'Target In Use',
                    content: html `<p>${currentState.error} Force delete?</p>`,
                    menuOptions: [
                        {
                            name: 'Force Delete',
                            action: async (modalArg) => {
                                await appstate.profilesTargetsStatePart.dispatchAction(appstate.deleteTargetAction, {
                                    id: target.id,
                                    force: true,
                                });
                                modalArg.destroy();
                            },
                        },
                        { name: 'Cancel', action: async (modalArg) => modalArg.destroy() },
                    ],
                });
            }
        }
        static {
            __runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return OpsViewNetworkTargets = _classThis;
})();
export { OpsViewNetworkTargets };
//# sourceMappingURL=data:application/json;base64, 