import * as i0 from '@angular/core';
import { Directive, Attribute, Inject, Optional, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PROP_STRING, PROP_BOOLEAN, StylableComponent, styler, register, PROP_TYPE, provideAsWidgetRef, WmComponentsModule } from '@wm/components/base';
import * as i1 from '@wm/core';
import { noop, setCSS } from '@wm/core';
import { startsWith, assign } from 'lodash-es';

const prefabProps = new Map([
    ['animation', PROP_STRING],
    ['class', { value: '', ...PROP_STRING }],
    ['name', PROP_STRING],
    ['show', { value: true, ...PROP_BOOLEAN }]
]);

const DEFAULT_CLS$1 = 'app-prefab';
const registeredPropsSet = new Set();
class PrefabDirective extends StylableComponent {
    constructor(inj, elRef, cdr, prefabName, explicitContext) {
        const widgetType = `wm-prefab-${prefabName}`;
        const WIDGET_CONFIG = { widgetType, hostClass: DEFAULT_CLS$1 };
        let resolveFn = noop;
        super(inj, WIDGET_CONFIG, explicitContext, new Promise(res => resolveFn = res));
        this.propsReady = resolveFn;
        this.prefabName = prefabName;
        this.widgetType = widgetType;
        this.name = elRef.nativeElement.getAttribute('name');
        styler(this.nativeElement, this);
        // Call on property change on name to set name attribute on element.
        this.registerReadyStateListener(() => {
            super.onPropertyChange('name', this.name);
        });
    }
    onStyleChange(key, nv, ov) {
        if (key === 'height') {
            setCSS(this.nativeElement, 'overflow', 'auto');
        }
    }
    setProps(config) {
        if (!config || !config.properties) {
            return;
        }
        if (!registeredPropsSet.has(this.widgetType)) {
            register(this.widgetType, this.prepareProps(config.properties));
        }
        this.propsReady();
    }
    handleEvent() {
        // do not call the super;
        // prevent events from getting registered
    }
    prepareProps(props = {}) {
        const propsMap = new Map(prefabProps);
        Object.entries(props).forEach(([k, v]) => {
            let type = PROP_TYPE.STRING;
            if (v.type === 'boolean') {
                type = PROP_TYPE.BOOLEAN;
            }
            else if (v.type === 'number') {
                type = PROP_TYPE.NUMBER;
            }
            else if (v.type !== 'string') {
                type = v.type;
            }
            // Do not set the 'bind:*' values
            propsMap.set(k, { type, value: startsWith(v.value, 'bind:') ? undefined : v.value });
        });
        registeredPropsSet.add(this.widgetType);
        return propsMap;
    }
    ngOnDestroy() {
        this.invokeEventCallback('destroy', { widget: this });
    }
    static { this.ɵfac = function PrefabDirective_Factory(t) { return new (t || PrefabDirective)(i0.ɵɵdirectiveInject(i0.Injector), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef), i0.ɵɵinjectAttribute('prefabname'), i0.ɵɵdirectiveInject('EXPLICIT_CONTEXT', 8)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: PrefabDirective, selectors: [["section", "wmPrefab", ""]], features: [i0.ɵɵProvidersFeature([
                provideAsWidgetRef(PrefabDirective)
            ]), i0.ɵɵInheritDefinitionFeature] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PrefabDirective, [{
        type: Directive,
        args: [{
                selector: 'section[wmPrefab]',
                providers: [
                    provideAsWidgetRef(PrefabDirective)
                ]
            }]
    }], () => [{ type: i0.Injector }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }, { type: undefined, decorators: [{
                type: Attribute,
                args: ['prefabname']
            }] }, { type: undefined, decorators: [{
                type: Inject,
                args: ['EXPLICIT_CONTEXT']
            }, {
                type: Optional
            }] }], null); })();

const registerProps = () => {
    register('wm-prefab-container', new Map([
        ['class', PROP_STRING],
        ['name', PROP_STRING],
        ['show', { value: true, ...PROP_BOOLEAN }],
    ]));
};

const DEFAULT_CLS = 'app-prefab-container full-height';
const WIDGET_CONFIG = {
    widgetType: 'wm-prefab-container',
    hostClass: DEFAULT_CLS
};
class PrefabContainerDirective extends StylableComponent {
    static { this.initializeProps = registerProps(); }
    constructor(inj, viewport, explicitContext) {
        super(inj, WIDGET_CONFIG, explicitContext);
        this.viewport = viewport;
        styler(this.nativeElement, this);
        this.registerDestroyListener(this.viewport.subscribe("resize" /* ViewportEvent.RESIZE */, data => this.callback('resize', data)));
        this.registerDestroyListener(this.viewport.subscribe("orientationchange" /* ViewportEvent.ORIENTATION_CHANGE */, data => this.callback('orientationchange', data)));
    }
    callback(eventName, locals) {
        locals = assign({ widget: this }, locals);
        this.invokeEventCallback(eventName, locals);
    }
    ngOnAttach() {
        this.callback('attach');
    }
    ngOnDetach() {
        this.callback('detach');
    }
    ngOnDestroy() {
        this.callback('destroy');
        super.ngOnDestroy();
    }
    static { this.ɵfac = function PrefabContainerDirective_Factory(t) { return new (t || PrefabContainerDirective)(i0.ɵɵdirectiveInject(i0.Injector), i0.ɵɵdirectiveInject(i1.Viewport), i0.ɵɵdirectiveInject('EXPLICIT_CONTEXT', 8)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: PrefabContainerDirective, selectors: [["", "wmPrefabContainer", ""]], features: [i0.ɵɵProvidersFeature([
                provideAsWidgetRef(PrefabContainerDirective)
            ]), i0.ɵɵInheritDefinitionFeature] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PrefabContainerDirective, [{
        type: Directive,
        args: [{
                selector: '[wmPrefabContainer]',
                providers: [
                    provideAsWidgetRef(PrefabContainerDirective)
                ]
            }]
    }], () => [{ type: i0.Injector }, { type: i1.Viewport }, { type: undefined, decorators: [{
                type: Inject,
                args: ['EXPLICIT_CONTEXT']
            }, {
                type: Optional
            }] }], null); })();

const components = [
    PrefabDirective,
    PrefabContainerDirective
];
class PrefabModule {
    static { this.ɵfac = function PrefabModule_Factory(t) { return new (t || PrefabModule)(); }; }
    static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: PrefabModule }); }
    static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [CommonModule,
            WmComponentsModule] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PrefabModule, [{
        type: NgModule,
        args: [{
                imports: [
                    CommonModule,
                    WmComponentsModule
                ],
                declarations: [...components],
                exports: [...components]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(PrefabModule, { declarations: [PrefabDirective,
        PrefabContainerDirective], imports: [CommonModule,
        WmComponentsModule], exports: [PrefabDirective,
        PrefabContainerDirective] }); })();

/**
 * Generated bundle index. Do not edit.
 */

export { PrefabContainerDirective, PrefabDirective, PrefabModule };
//# sourceMappingURL=index.mjs.map
