import { __esDecorate, __runInitializers } from "tslib";
import { render } from 'lit';
import { bound } from '../../decorators/bound.js';
import deepClone from '@api-client/json/core/Clone.js';
import deepEqual from '@api-client/json/core/Equal.js';
import formStyles from '../internals/styles/form_styles.js';
import commonStyles from './common.js';
import { adoptStyles } from '../../lib/Dom.js';
import { DialogBuilder } from '../../builders/DialogBuilder.js';
import '../../md/checkbox/ui-checkbox.js';
import '../../md/text-field/ui-outlined-text-field.js';
import '../../md/select/ui-select.js';
import '../../md/select/ui-option.js';
let TaxonomyDialog = (() => {
    let _classSuper = DialogBuilder;
    let _instanceExtraInitializers = [];
    let _formCallback_decorators;
    let _handleBooleanChange_decorators;
    let _handleTextChange_decorators;
    let _handleSelectChange_decorators;
    return class TaxonomyDialog extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _formCallback_decorators = [bound];
            _handleBooleanChange_decorators = [bound];
            _handleTextChange_decorators = [bound];
            _handleSelectChange_decorators = [bound];
            __esDecorate(this, null, _formCallback_decorators, { kind: "method", name: "formCallback", static: false, private: false, access: { has: obj => "formCallback" in obj, get: obj => obj.formCallback }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _handleBooleanChange_decorators, { kind: "method", name: "handleBooleanChange", static: false, private: false, access: { has: obj => "handleBooleanChange" in obj, get: obj => obj.handleBooleanChange }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _handleTextChange_decorators, { kind: "method", name: "handleTextChange", static: false, private: false, access: { has: obj => "handleTextChange" in obj, get: obj => obj.handleTextChange }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _handleSelectChange_decorators, { kind: "method", name: "handleSelectChange", static: false, private: false, access: { has: obj => "handleSelectChange" in obj, get: obj => obj.handleSelectChange }, metadata: _metadata }, null, _instanceExtraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        static title = 'Configure Taxonomy';
        /**
         * The configuration object for the taxonomy.
         * This object is used to store the state of the form.
         */
        config = __runInitializers(this, _instanceExtraInitializers);
        /**
         * The original configuration object used to track changes.
         * This is a deep clone of the config passed to the constructor.
         */
        originalConfig;
        constructor(config) {
            super();
            this.config = config;
            this.modal = true;
            this.originalConfig = deepClone(config);
            this.renderRoot = this.createForm();
            this.body = document.createElement('div');
            this.body.classList.add('form-inputs');
            this.classes = ['taxonomy-dialog', 'message', this.constructor.name.toLocaleLowerCase()];
            this.positiveAction = {
                label: 'Save',
                value: 'confirm',
                type: 'submit',
            };
            this.negativeAction = {
                label: 'Cancel',
                value: 'dismiss',
            };
            this.title = this.constructor.title || 'Configure Taxonomy';
            // Set up unsaved changes detection
            this.setUnsavedChangesDetection({
                hasUnsavedChanges: () => this.hasUnsavedChanges(),
                warningTitle: 'Unsaved Changes',
                warningMessage: 'You have unsaved changes that will be lost if you continue. Are you sure you want to discard your changes?',
                discardLabel: 'Discard Changes',
                keepEditingLabel: 'Keep Editing',
            });
        }
        /**
         * Checks if there are unsaved changes by comparing the current config
         * with the original config.
         */
        hasUnsavedChanges() {
            return !deepEqual(this.config, this.originalConfig);
        }
        createForm() {
            const form = document.createElement('form');
            form.classList.add('taxonomy-form');
            form.addEventListener('submit', this.formCallback);
            form.addEventListener('reset', this.handleDialogClosed);
            return form;
        }
        async formCallback(event) {
            event.preventDefault();
            this.dispose();
        }
        /**
         * Removes the dialog from the DOM and removes all listeners.
         * The dialog does not call any lifecycle methods.
         */
        dispose() {
            super.dispose();
            this.renderRoot?.removeEventListener('submit', this.formCallback);
            this.renderRoot?.removeEventListener('reset', this.handleDialogClosed);
            this.renderRoot?.remove();
            this.renderRoot = undefined;
        }
        async run() {
            if (!this.renderRoot) {
                throw new Error('The form was disposed and cannot be run again.');
            }
            adoptStyles(formStyles, commonStyles);
            document.body.appendChild(this.renderRoot);
            this.open();
            this.requestUpdate();
        }
        requestUpdate() {
            if (this.body) {
                render(this.render(), this.body, {
                    host: this,
                });
            }
        }
        /**
         * A handler for any of the checkboxes in the form.
         * Updates the `config`
         */
        handleBooleanChange(e) {
            const checkbox = e.target;
            const { name = '', checked } = checkbox;
            const cnf = this.config;
            cnf[name] = !!checked;
            this.requestUpdate();
        }
        handleTextChange(e) {
            const input = e.target;
            const { name, value, type } = input;
            const cnf = this.config;
            if (type === 'number') {
                cnf[name] = Number(value);
            }
            else {
                cnf[name] = value;
            }
            this.requestUpdate();
        }
        handleSelectChange(e) {
            const select = e.target;
            const { name, value } = select;
            if (!name || !value) {
                return;
            }
            const cnf = this.config;
            cnf[name] = value;
            this.requestUpdate();
        }
        listComputedProperties(entity, opts = {}) {
            const result = [];
            const { exclude = [], types = [] } = opts;
            for (const prop of entity.properties) {
                if (exclude.includes(prop.key)) {
                    // Skip the current property itself and excluded properties
                    continue;
                }
                // Only include properties that match the specified types
                if (!types.length || types.includes(prop.type)) {
                    result.push(prop);
                }
            }
            for (const parent of entity.parents) {
                for (const prop of parent.properties) {
                    if (!types.length || types.includes(prop.type)) {
                        result.push(prop);
                    }
                }
            }
            return result;
        }
    };
})();
export { TaxonomyDialog };
//# sourceMappingURL=TaxonomyDialog.js.map