import { __esDecorate, __runInitializers } from "tslib";
import { html, nothing } from 'lit';
import { ApiModelingScreen } from './ApiModelingScreen.js';
import { state } from 'lit/decorators.js';
import { live } from 'lit/directives/live.js';
import { renderHelp } from '../../help/render.js';
let ApiModelingDescriptionScreen = (() => {
    let _classSuper = ApiModelingScreen;
    let _isCustomLicense_decorators;
    let _isCustomLicense_initializers = [];
    let _isCustomLicense_extraInitializers = [];
    let _isContactEnabled_decorators;
    let _isContactEnabled_initializers = [];
    let _isContactEnabled_extraInitializers = [];
    return class ApiModelingDescriptionScreen extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _isCustomLicense_decorators = [state()];
            _isContactEnabled_decorators = [state()];
            __esDecorate(this, null, _isCustomLicense_decorators, { kind: "accessor", name: "isCustomLicense", static: false, private: false, access: { has: obj => "isCustomLicense" in obj, get: obj => obj.isCustomLicense, set: (obj, value) => { obj.isCustomLicense = value; } }, metadata: _metadata }, _isCustomLicense_initializers, _isCustomLicense_extraInitializers);
            __esDecorate(this, null, _isContactEnabled_decorators, { kind: "accessor", name: "isContactEnabled", static: false, private: false, access: { has: obj => "isContactEnabled" in obj, get: obj => obj.isContactEnabled, set: (obj, value) => { obj.isContactEnabled = value; } }, metadata: _metadata }, _isContactEnabled_initializers, _isContactEnabled_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #isCustomLicense_accessor_storage = __runInitializers(this, _isCustomLicense_initializers, false
        /**
         * Whether contact information is enabled.
         */
        );
        /**
         * Whether a custom license name is being used.
         */
        get isCustomLicense() { return this.#isCustomLicense_accessor_storage; }
        set isCustomLicense(value) { this.#isCustomLicense_accessor_storage = value; }
        #isContactEnabled_accessor_storage = (__runInitializers(this, _isCustomLicense_extraInitializers), __runInitializers(this, _isContactEnabled_initializers, false));
        /**
         * Whether contact information is enabled.
         */
        get isContactEnabled() { return this.#isContactEnabled_accessor_storage; }
        set isContactEnabled(value) { this.#isContactEnabled_accessor_storage = value; }
        handleModelChange(model) {
            this.checkCustomLicense(model);
            this.checkContactEnabled(model);
        }
        /**
         * Checks if the current license is a custom one and updates the state.
         * @param model The current API model.
         */
        checkCustomLicense(model = this.model) {
            if (!model?.license?.name) {
                this.isCustomLicense = false;
                return;
            }
            const commonLicenses = ['MIT', 'Apache-2.0', 'GPL-3.0'];
            this.isCustomLicense = !commonLicenses.includes(model.license.name);
        }
        /**
         * Checks if contact information is available and updates the state.
         * @param model The current API model.
         */
        checkContactEnabled(model) {
            const currentModel = model || this.model;
            this.isContactEnabled = !!currentModel?.contact;
        }
        handleMetaChange(e) {
            const model = this.model;
            if (!model) {
                return;
            }
            const node = e.target;
            if (node.value && !node.reportValidity()) {
                return;
            }
            const { name, value } = node;
            switch (name) {
                case 'name':
                case 'version':
                case 'description':
                    if (name === 'name' && !value) {
                        return;
                    }
                    model.info[name] = value;
                    break;
                case 'termsOfService':
                    model.termsOfService = value || undefined;
                    break;
                case 'licenseUrl':
                    if (!model.license) {
                        model.license = { name: '' };
                    }
                    model.license.url = value || undefined;
                    break;
                case 'customLicenseName':
                    if (!model.license) {
                        model.license = { name: '' };
                    }
                    model.license.name = value;
                    break;
                case 'contactName':
                    if (!model.contact)
                        model.contact = {};
                    model.contact.name = value || undefined;
                    break;
                case 'contactEmail':
                    if (!model.contact)
                        model.contact = {};
                    model.contact.email = value || undefined;
                    break;
                case 'contactUrl':
                    if (!model.contact)
                        model.contact = {};
                    model.contact.url = value || undefined;
                    break;
                default:
                    return;
            }
            this.model?.notifyChange();
            this.requestUpdate();
        }
        handleLicenseSelect(e) {
            const model = this.model;
            if (!model) {
                return;
            }
            const select = e.target;
            const { value } = select;
            if (value === 'custom') {
                this.isCustomLicense = true;
                const isStandard = model.license && ['MIT', 'Apache-2.0', 'GPL-3.0'].includes(model.license.name);
                if (!model.license || isStandard) {
                    model.license = { name: '' };
                }
            }
            else if (value) {
                this.isCustomLicense = false;
                model.license = {
                    name: value,
                    url: this.getLicenseUrl(value),
                };
            }
            else {
                this.isCustomLicense = false;
                model.license = undefined;
            }
            this.model?.notifyChange();
            this.requestUpdate();
        }
        handleContactToggle(e) {
            const model = this.model;
            if (!model) {
                return;
            }
            const toggle = e.target;
            this.isContactEnabled = !!toggle.checked;
            if (this.isContactEnabled) {
                if (!model.contact) {
                    model.contact = {};
                }
            }
            else {
                model.contact = undefined;
            }
            this.model?.notifyChange();
            this.requestUpdate();
        }
        getLicenseUrl(name) {
            switch (name) {
                case 'MIT':
                    return 'https://opensource.org/licenses/MIT';
                case 'Apache-2.0':
                    return 'https://opensource.org/licenses/Apache-2.0';
                case 'GPL-3.0':
                    return 'https://www.gnu.org/licenses/gpl-3.0';
                default:
                    return undefined;
            }
        }
        render() {
            const model = this.model;
            if (!model) {
                return nothing;
            }
            return html `${this.renderMetaFields(model)}<ui-divider type="full"></ui-divider>${this.renderLegalFields(model)}<ui-divider type="full"></ui-divider>${this.renderContactSection(model)}`;
        }
        renderMetaFields(model) {
            return html `
      <div class="form-section meta-section">
        <div class="row">
          <ui-outlined-text-field
            class="input"
            name="name"
            .value="${live(model.info.name || '')}"
            label="Name"
            required
            minLength="2"
            @change="${this.handleMetaChange}"
          ></ui-outlined-text-field>
          ${renderHelp('api', 'description:name-input')}
        </div>
        <div class="row">
          <ui-outlined-text-field
            class="input"
            name="description"
            .value="${live(model.info.description || '')}"
            label="Description"
            @change="${this.handleMetaChange}"
          ></ui-outlined-text-field>
          ${renderHelp('api', 'description:description-input')}
        </div>
        <div class="row">
          <ui-outlined-text-field
            class="input"
            name="version"
            .value="${live(model.info.version || 'v1')}"
            label="Version"
            required
            @change="${this.handleMetaChange}"
          ></ui-outlined-text-field>
          ${renderHelp('api', 'description:version-input')}
        </div>
      </div>
    `;
        }
        renderLegalFields(model) {
            const licenseName = model.license?.name || '';
            const licenseValue = this.isCustomLicense ? 'custom' : licenseName;
            return html `
      <div class="form-section legal-section">
        <div class="row">
          <ui-outlined-text-field
            class="input"
            name="termsOfService"
            .value="${live(model.termsOfService || '')}"
            label="Terms of Service (URL)"
            @change="${this.handleMetaChange}"
            type="url"
          ></ui-outlined-text-field>
          ${renderHelp('api', 'description:terms-of-service-input')}
        </div>
        <div class="row">
          <ui-select
            class="input"
            label="License"
            name="license"
            .value="${licenseValue}"
            @change="${this.handleLicenseSelect}"
          >
            <ui-option value=""><div>None</div></ui-option>
            <ui-option value="MIT"><div>MIT</div></ui-option>
            <ui-option value="Apache-2.0"><div>Apache 2.0</div></ui-option>
            <ui-option value="GPL-3.0"><div>GNU GPLv3</div></ui-option>
            <ui-option value="custom"><div>Custom</div></ui-option>
          </ui-select>
          ${renderHelp('api', 'description:license-input')}
        </div>
        ${this.isCustomLicense ? this.renderCustomLicenseName(model) : nothing}
        ${model.license ? this.renderLicenseUrl(model) : nothing}
      </div>
    `;
        }
        renderCustomLicenseName(model) {
            return html `
      <div class="row">
        <ui-outlined-text-field
          class="input"
          name="customLicenseName"
          .value="${live(model.license?.name || '')}"
          label="Custom License Name"
          @change="${this.handleMetaChange}"
          required
        ></ui-outlined-text-field>
        ${renderHelp('api', 'description:custom-license-name-input')}
      </div>
    `;
        }
        renderLicenseUrl(model) {
            return html `
      <div class="row">
        <ui-outlined-text-field
          class="input"
          name="licenseUrl"
          .value="${live(model.license?.url || '')}"
          label="License URL"
          @change="${this.handleMetaChange}"
          type="url"
        ></ui-outlined-text-field>
        ${renderHelp('api', 'description:license-url-input')}
      </div>
    `;
        }
        renderContactSection(model) {
            return html `
      <div class="form-section contact-section">
        <div class="row">
          <div class="input enum">
            <ui-switch
              .checked="${this.isContactEnabled}"
              id="contactEnabled"
              @change="${this.handleContactToggle}"
            ></ui-switch>
            <label for="contactEnabled" class="body-medium">Contact Information</label>
          </div>
          ${renderHelp('api', 'description:contact-info-toggle')}
        </div>
        ${this.isContactEnabled ? this.renderContactFields(model) : nothing}
      </div>
    `;
        }
        renderContactFields(model) {
            const contact = model.contact || {};
            return html `
      <div class="row">
        <ui-outlined-text-field
          class="input"
          name="contactName"
          .value="${live(contact.name || '')}"
          label="Contact Name"
          @change="${this.handleMetaChange}"
        ></ui-outlined-text-field>
        ${renderHelp('api', 'description:contact-name-input')}
      </div>
      <div class="row">
        <ui-outlined-text-field
          class="input"
          name="contactEmail"
          .value="${live(contact.email || '')}"
          label="Contact Email"
          type="email"
          @change="${this.handleMetaChange}"
        ></ui-outlined-text-field>
        ${renderHelp('api', 'description:contact-email-input')}
      </div>
      <div class="row">
        <ui-outlined-text-field
          class="input"
          name="contactUrl"
          .value="${live(contact.url || '')}"
          label="Contact URL"
          type="url"
          @change="${this.handleMetaChange}"
        ></ui-outlined-text-field>
        ${renderHelp('api', 'description:contact-url-input')}
      </div>
    `;
        }
        constructor() {
            super(...arguments);
            __runInitializers(this, _isContactEnabled_extraInitializers);
        }
    };
})();
/**
 * A screen for editing API description information.
 * Used in API modeling.
 */
export default ApiModelingDescriptionScreen;
//# sourceMappingURL=ApiModelingDescriptionScreen.js.map