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, property, cssManager, } from '@design.estate/dees-element';
import * as interfaces from '../../../dist_ts_interfaces/index.js';
/**
 * Reactive credential form for a DNS provider. Renders the type picker
 * and the credential fields for the currently-selected type.
 *
 * Provider-agnostic — driven entirely by `dnsProviderTypeDescriptors` from
 * `ts_interfaces/data/dns-provider.ts`. Adding a new provider type means
 * appending one entry to the descriptors array; this form picks it up
 * automatically.
 *
 * Usage:
 *
 *     const formEl = document.createElement('dns-provider-form');
 *     formEl.providerName = 'My provider';
 *     // ... pass element into a DeesModal as content ...
 *     // on submit:
 *     const data = formEl.collectData();
 *     // → { name, type, credentials }
 *
 * In edit mode, set `lockType = true` so the user cannot change provider
 * type after creation (credentials shapes don't transfer between types).
 */
let DnsProviderForm = (() => {
    let _classDecorators = [customElement('dns-provider-form')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = DeesElement;
    let _providerName_decorators;
    let _providerName_initializers = [];
    let _providerName_extraInitializers = [];
    let _selectedType_decorators;
    let _selectedType_initializers = [];
    let _selectedType_extraInitializers = [];
    let _lockType_decorators;
    let _lockType_initializers = [];
    let _lockType_extraInitializers = [];
    let _credentialsHint_decorators;
    let _credentialsHint_initializers = [];
    let _credentialsHint_extraInitializers = [];
    let _credentialValues_decorators;
    let _credentialValues_initializers = [];
    let _credentialValues_extraInitializers = [];
    var DnsProviderForm = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _providerName_decorators = [property({ type: String })];
            _selectedType_decorators = [state()];
            _lockType_decorators = [property({ type: Boolean })];
            _credentialsHint_decorators = [property({ type: String })];
            _credentialValues_decorators = [state()];
            __esDecorate(this, null, _providerName_decorators, { kind: "accessor", name: "providerName", static: false, private: false, access: { has: obj => "providerName" in obj, get: obj => obj.providerName, set: (obj, value) => { obj.providerName = value; } }, metadata: _metadata }, _providerName_initializers, _providerName_extraInitializers);
            __esDecorate(this, null, _selectedType_decorators, { kind: "accessor", name: "selectedType", static: false, private: false, access: { has: obj => "selectedType" in obj, get: obj => obj.selectedType, set: (obj, value) => { obj.selectedType = value; } }, metadata: _metadata }, _selectedType_initializers, _selectedType_extraInitializers);
            __esDecorate(this, null, _lockType_decorators, { kind: "accessor", name: "lockType", static: false, private: false, access: { has: obj => "lockType" in obj, get: obj => obj.lockType, set: (obj, value) => { obj.lockType = value; } }, metadata: _metadata }, _lockType_initializers, _lockType_extraInitializers);
            __esDecorate(this, null, _credentialsHint_decorators, { kind: "accessor", name: "credentialsHint", static: false, private: false, access: { has: obj => "credentialsHint" in obj, get: obj => obj.credentialsHint, set: (obj, value) => { obj.credentialsHint = value; } }, metadata: _metadata }, _credentialsHint_initializers, _credentialsHint_extraInitializers);
            __esDecorate(this, null, _credentialValues_decorators, { kind: "accessor", name: "credentialValues", static: false, private: false, access: { has: obj => "credentialValues" in obj, get: obj => obj.credentialValues, set: (obj, value) => { obj.credentialValues = value; } }, metadata: _metadata }, _credentialValues_initializers, _credentialValues_extraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            DnsProviderForm = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #providerName_accessor_storage = __runInitializers(this, _providerName_initializers, '');
        /** Pre-populated provider name. */
        get providerName() { return this.#providerName_accessor_storage; }
        set providerName(value) { this.#providerName_accessor_storage = value; }
        #selectedType_accessor_storage = (__runInitializers(this, _providerName_extraInitializers), __runInitializers(this, _selectedType_initializers, interfaces.data.dnsProviderTypeDescriptors.find((d) => d.type !== 'dcrouter')?.type ??
            'cloudflare'));
        /**
         * Currently selected provider type. Initialized to the first user-creatable
         * descriptor; caller can override before mounting (e.g. for edit dialogs).
         * The built-in 'dcrouter' pseudo-provider is excluded from the picker —
         * operators cannot create another one.
         */
        get selectedType() { return this.#selectedType_accessor_storage; }
        set selectedType(value) { this.#selectedType_accessor_storage = value; }
        #lockType_accessor_storage = (__runInitializers(this, _selectedType_extraInitializers), __runInitializers(this, _lockType_initializers, false));
        /** When true, hide the type picker — used in edit dialogs. */
        get lockType() { return this.#lockType_accessor_storage; }
        set lockType(value) { this.#lockType_accessor_storage = value; }
        #credentialsHint_accessor_storage = (__runInitializers(this, _lockType_extraInitializers), __runInitializers(this, _credentialsHint_initializers, ''));
        /**
         * Help text shown above credentials. Useful for edit dialogs to indicate
         * that fields can be left blank to keep current values.
         */
        get credentialsHint() { return this.#credentialsHint_accessor_storage; }
        set credentialsHint(value) { this.#credentialsHint_accessor_storage = value; }
        #credentialValues_accessor_storage = (__runInitializers(this, _credentialsHint_extraInitializers), __runInitializers(this, _credentialValues_initializers, {}));
        /** Internal map of credential field values, keyed by the descriptor's `key`. */
        get credentialValues() { return this.#credentialValues_accessor_storage; }
        set credentialValues(value) { this.#credentialValues_accessor_storage = value; }
        static styles = [
            cssManager.defaultStyles,
            css `
      :host {
        display: block;
      }

      .field {
        margin-bottom: 12px;
      }

      .credentialsHint {
        font-size: 12px;
        opacity: 0.7;
        margin-bottom: 12px;
      }
    `,
        ];
        render() {
            // Exclude the built-in 'dcrouter' pseudo-provider from the type picker —
            // operators cannot create another one, it's surfaced at read time by the
            // backend handler instead.
            const descriptors = interfaces.data.dnsProviderTypeDescriptors.filter((d) => d.type !== 'dcrouter');
            const descriptor = interfaces.data.getDnsProviderTypeDescriptor(this.selectedType);
            return html `
      <dees-form>
        <div class="field">
          <dees-input-text
            .key=${'name'}
            .label=${'Provider name'}
            .value=${this.providerName}
            .required=${true}
          ></dees-input-text>
        </div>

        ${this.lockType
                ? html `
              <div class="field">
                <dees-input-text
                  .key=${'__type_display'}
                  .label=${'Type'}
                  .infoText=${descriptor?.description || ''}
                  .value=${descriptor?.displayName ?? this.selectedType}
                  .disabled=${true}
                ></dees-input-text>
              </div>
            `
                : html `
              <div class="field">
                <dees-input-dropdown
                  .key=${'__type'}
                  .label=${'Provider type'}
                  .infoText=${descriptor?.description || ''}
                  .options=${descriptors.map((d) => ({ option: d.displayName, key: d.type }))}
                  .selectedOption=${descriptor
                    ? { option: descriptor.displayName, key: descriptor.type }
                    : undefined}
                  @selectedOption=${(e) => {
                    const newType = e.detail?.key;
                    if (newType && newType !== this.selectedType) {
                        this.selectedType = newType;
                        this.credentialValues = {};
                    }
                }}
                ></dees-input-dropdown>
              </div>
            `}
        ${descriptor
                ? html `
              ${this.credentialsHint
                    ? html `<div class="credentialsHint">${this.credentialsHint}</div>`
                    : ''}
              ${descriptor.credentialFields.map((f) => html `
                  <div class="field">
                    <dees-input-text
                      .key=${f.key}
                      .label=${f.label}
                      .description=${f.helpText || ''}
                      .required=${f.required && !this.lockType}
                    ></dees-input-text>
                  </div>
                `)}
            `
                : html `<p>No provider types registered.</p>`}
      </dees-form>
    `;
        }
        /**
         * Read the form values and assemble the create/update payload.
         * Returns the typed credentials object built from the descriptor's keys.
         */
        async collectData() {
            const form = this.shadowRoot?.querySelector('dees-form');
            if (!form)
                return null;
            const data = await form.collectFormData();
            const descriptor = interfaces.data.getDnsProviderTypeDescriptor(this.selectedType);
            if (!descriptor)
                return null;
            // Build the credentials object from the descriptor's field keys.
            const credsBody = {};
            let credentialsTouched = false;
            for (const f of descriptor.credentialFields) {
                const value = data[f.key];
                if (value !== undefined && value !== null && String(value).length > 0) {
                    credsBody[f.key] = String(value);
                    credentialsTouched = true;
                }
            }
            // The discriminator goes on the credentials object so the backend
            // factory and the discriminated union both stay happy.
            const credentials = {
                type: this.selectedType,
                ...credsBody,
            };
            return {
                name: String(data.name ?? ''),
                type: this.selectedType,
                credentials,
                credentialsTouched,
            };
        }
        constructor() {
            super(...arguments);
            __runInitializers(this, _credentialValues_extraInitializers);
        }
        static {
            __runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return DnsProviderForm = _classThis;
})();
export { DnsProviderForm };
//# sourceMappingURL=data:application/json;base64, 