import { inject, Injector } from '@angular/core';
import { LAYOUT_COMPONENT_TRANSFORM } from '@anglr/dynamic/layout';
import { RelationsChangeDetector, RelationsComponentManager, RelationsDebugger, RelationsManager, RelationsProcessor } from '@anglr/dynamic/relations';
import { LOGGER } from '@anglr/common';
/**
 * Service that handles storing, obtaining of placeholder configuration, also correct rendering of placeholder or placeholder container
 */
export class PlaceholderHandler {
    /**
     * Gets id of attached component
     */
    get id() {
        return (this.ɵid ?? (this.ɵid = this.injector.get(this.componentWithIdType).id));
    }
    /**
     * Gets indication whether is this handler attached to custom component
     */
    get isCustomComponent() {
        return !this.ɵisPlaceholder;
    }
    /**
     * Gets indication whether is this handler attached to placeholder
     */
    get isPlaceholder() {
        return this.ɵisPlaceholder;
    }
    /**
     * Gets nearest handler for custom component
     */
    get nearestCustomComponent() {
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        let handler = this;
        do {
            if (handler.isCustomComponent) {
                return handler;
            }
        } while ((handler = handler.parent));
        return null;
    }
    /**
     * Gets nearest handler for custom component that is in design mode
     */
    get nearestDesignCustomComponent() {
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        let handler = this;
        do {
            if (handler.isCustomComponent && handler.designMode) {
                return handler;
            }
        } while ((handler = handler.parent));
        return null;
    }
    //######################### public properties #########################
    /**
     * Gets instance of parent placeholder handler
     */
    get parent() {
        return this.ɵparent;
    }
    /**
     * Gets indication whether is attached component in design mode
     */
    get designMode() {
        return (this.ɵdesignMode ?? (this.ɵdesignMode = this.ɵisPlaceholder ?
            !!this.injector.get(LAYOUT_COMPONENT_TRANSFORM, null, { optional: true }) :
            !!this.injector.get(LAYOUT_COMPONENT_TRANSFORM, null, { skipSelf: true, optional: true, })));
    }
    /**
     * Gets indication whether display placeholder container or just placeholder
     */
    get placeholderContainer() {
        return !(!this.parent || (this.parent.isPlaceholder && this.parent.designMode));
    }
    /**
     * Gets layout designer component transform function
     */
    get layoutDesignerComponentTransform() {
        //current transform
        const transform = this.injector.get(LAYOUT_COMPONENT_TRANSFORM, null, { skipSelf: true, optional: true });
        //current or parent custom component transform
        const result = transform ??
            this.findRelatedCustomComponentHandler()?.injector?.get(LAYOUT_COMPONENT_TRANSFORM, null, { skipSelf: true, optional: true }) ??
            null;
        return result;
    }
    /**
     * Relations component manager instance that is used for placeholders
     */
    get relationsComponentManager() {
        return this.getParentCustomComponentHandler()?.injector.get(RelationsComponentManager, undefined, { skipSelf: true }) ?? null;
    }
    /**
     * Relations processor instance that is used for placeholders
     */
    get relationsProcessor() {
        return this.getParentCustomComponentHandler()?.injector.get(RelationsProcessor, undefined, { skipSelf: true }) ?? null;
    }
    /**
     * Relations change detector instance that is used for placeholders
     */
    get relationsChangeDetector() {
        return this.getParentCustomComponentHandler()?.injector.get(RelationsChangeDetector, undefined, { skipSelf: true }) ?? null;
    }
    /**
     * Relations debugger instance that is used for placeholders
     */
    get relationsDebugger() {
        return this.getParentCustomComponentHandler()?.injector.get(RelationsDebugger, undefined, { skipSelf: true }) ?? null;
    }
    /**
     * Relations manager instance that is used for placeholders
     */
    get relationsManager() {
        return this.getParentCustomComponentHandler()?.injector.get(RelationsManager, undefined, { skipSelf: true }) ?? null;
    }
    //######################### constructor #########################
    constructor(componentWithIdType, customComponent) {
        this.componentWithIdType = componentWithIdType;
        this.customComponent = customComponent;
        /**
         * Instance of parent placeholder handler
         */
        this.ɵparent = inject(PlaceholderHandler, { optional: true, skipSelf: true, });
        /**
         * Injector that is assigned with placeholder handler
         */
        this.injector = inject(Injector);
        /**
         * Instance of logger used for logging
         */
        this.logger = inject(LOGGER);
        this.ɵisPlaceholder = !this.customComponent;
    }
    //######################### public methods #########################
    /**
     * Gets options for placeholder using its id
     * @param id - Id of placeholder for which are options obtained
     */
    getOptions(id) {
        //is placeholder
        if (this.isPlaceholder) {
            if (!this.parent) {
                return null;
            }
            return this.parent.getOptions(id);
        }
        if (this.isCustomComponent && this.customComponent?.options?.placeholderContainers?.[id]) {
            return this.customComponent.options.placeholderContainers[id];
        }
        if (!this.parent) {
            return null;
        }
        return this.parent.getOptions(id);
    }
    /**
     * Initialize options for placeholder container
     */
    initOptions() {
        var _a;
        const custom = this.nearestDesignCustomComponent;
        if (!custom?.designMode || !custom.customComponent) {
            this.logger.error('PlaceholderHandler: closes parent custom component should be in editation mode!');
            return;
        }
        if (!custom.customComponent.options) {
            this.logger.error('PlaceholderHandler: missing custom component options!');
            return;
        }
        //if options does not exists create new one
        if (!custom.customComponent.options.placeholderContainers?.[this.id]) {
            const containerId = `placeholderContainer-${custom.id}-${this.id}`;
            (_a = custom.customComponent.options).placeholderContainers ?? (_a.placeholderContainers = {});
            custom.customComponent.options.placeholderContainers[this.id] =
                {
                    id: containerId,
                    name: 'placeholderContainer',
                    package: 'custom-components',
                    displayName: containerId,
                    options: {},
                };
        }
    }
    //######################### protected methods #########################
    /**
     * Finds if there is any related handler with attached custom component
     */
    findRelatedCustomComponentHandler() {
        let placeholders = 0;
        let customComponents = 0;
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        let handler = this;
        if (!handler.parent) {
            return null;
        }
        do {
            if (handler.parent.isCustomComponent && handler.parent.designMode) {
                if (placeholders == customComponents) {
                    return handler.parent;
                }
                return null;
            }
            if (handler.parent.isCustomComponent) {
                customComponents++;
            }
            else {
                placeholders++;
            }
        } while ((handler = handler.parent));
        return null;
    }
    /**
     * Gets handler, which is attached to custom component which owns this placeholder container
     */
    getParentCustomComponentHandler() {
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        let handler = this;
        if (!handler.parent) {
            throw new Error('PlaceholderHandler: parent must be present to work with relations!');
        }
        do {
            //skip placeholder
            if (handler.isPlaceholder) {
                continue;
            }
            if (handler.customComponent?.options?.placeholderContainers?.[this.id]) {
                return handler;
            }
        } while ((handler = handler.parent));
        this.logger.error('PlaceholderHandler: unable to find owning custom component for {{@id}}', { id: this.id });
        return null;
    }
}
//# sourceMappingURL=placeholderHandler.service.js.map