System.register(["@angular/core", "@angular/platform-browser", "@angular/forms", "rxjs", "rxjs/operators", "toastify-js", "@stratusjs/core/misc", "@stratusjs/runtime/stratus", "@stratusjs/core/environment", "@stratusjs/core/events/eventManager", "../core/root.component", "lodash", "@stratusjs/angularjs/services/registry"], function (exports_1, context_1) {
    "use strict";
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __metadata = (this && this.__metadata) || function (k, v) {
        if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
    };
    var core_1, platform_browser_1, forms_1, rxjs_1, operators_1, toastify_js_1, misc_1, stratus_1, environment_1, eventManager_1, root_component_1, lodash_1, registry_1, systemDir, moduleName, min, localDir, TimezoneSelectorComponent;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (platform_browser_1_1) {
                platform_browser_1 = platform_browser_1_1;
            },
            function (forms_1_1) {
                forms_1 = forms_1_1;
            },
            function (rxjs_1_1) {
                rxjs_1 = rxjs_1_1;
            },
            function (operators_1_1) {
                operators_1 = operators_1_1;
            },
            function (toastify_js_1_1) {
                toastify_js_1 = toastify_js_1_1;
            },
            function (misc_1_1) {
                misc_1 = misc_1_1;
            },
            function (stratus_1_1) {
                stratus_1 = stratus_1_1;
            },
            function (environment_1_1) {
                environment_1 = environment_1_1;
            },
            function (eventManager_1_1) {
                eventManager_1 = eventManager_1_1;
            },
            function (root_component_1_1) {
                root_component_1 = root_component_1_1;
            },
            function (lodash_1_1) {
                lodash_1 = lodash_1_1;
            },
            function (registry_1_1) {
                registry_1 = registry_1_1;
            }
        ],
        execute: function () {
            systemDir = '@stratusjs/angular';
            moduleName = 'timezone-selector';
            min = !environment_1.cookie('env') ? '.min' : '';
            localDir = `${stratus_1.Stratus.BaseUrl}${boot.configuration.paths[`${systemDir}/*`].replace(/[^/]*$/, '').replace(/\/dist\/$/, '/src/')}`;
            TimezoneSelectorComponent = class TimezoneSelectorComponent extends root_component_1.RootComponent {
                elementRef;
                ref;
                sanitizer;
                title = moduleName + '_component';
                target;
                targetSuffix;
                id;
                manifest;
                decouple;
                direct;
                api;
                urlRoot;
                type;
                property;
                endpoint;
                Stratus = stratus_1.Stratus;
                registry = new registry_1.Registry();
                fetched;
                data;
                model;
                dataSub;
                onChange = new rxjs_1.Subject();
                subscriber;
                label = 'Select Timezone';
                timezone;
                currentTimezone;
                inputTimezone = new forms_1.FormControl('');
                timezoneList = [];
                filteredTimezones;
                initialized = false;
                constructor(elementRef, ref, sanitizer) {
                    super();
                    this.elementRef = elementRef;
                    this.ref = ref;
                    this.sanitizer = sanitizer;
                    this.uid = misc_1.safeUniqueId('sa', moduleName, 'component');
                    stratus_1.Stratus.Instances[this.uid] = this;
                    this.timezoneList = Intl.supportedValuesOf('timeZone');
                    this.currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
                    this.hydrate(elementRef, sanitizer, ["title", "target", "targetSuffix", "id", "manifest", "decouple", "direct", "api", "urlRoot", "type", "property", "endpoint", "Stratus", "registry", "fetched", "data", "model", "dataSub", "onChange", "subscriber", "label", "timezone", "currentTimezone", "inputTimezone", "timezoneList", "filteredTimezones", "initialized", "elementRef", "ref", "sanitizer", "ngOnInit", "updateLabel", "_filter", "fetchData", "dataDefer", "dataRef", "valueChanged", "handleObservableError", "timezoneSelected", "hydrate", "uid", "disableRefresh", "stagger", "reloading", "refresh", "ngOnDestroy"]);
                    this.filteredTimezones = this.inputTimezone.valueChanges.pipe(operators_1.startWith(''), operators_1.map(value => this._filter(value || '')));
                    this.fetchData()
                        .then(data => {
                        if (!data || !(data instanceof eventManager_1.EventManager)) {
                            console.warn('Unable to bind data from Registry!');
                            return;
                        }
                        const onDataChange = () => {
                            if (!data.completed) {
                                return;
                            }
                            this.dataDefer(this.subscriber);
                        };
                        data.on('change', onDataChange);
                        onDataChange();
                    });
                    this.dataSub = new rxjs_1.Observable(subscriber => {
                        return this.dataDefer(subscriber);
                    });
                    this.dataSub.pipe(operators_1.debounce(() => rxjs_1.timer(500)), operators_1.catchError(this.handleObservableError)).subscribe(evt => {
                        if (this.inputTimezone.value === evt) {
                            if (this.initialized) {
                                this.refresh().then();
                            }
                            return;
                        }
                        this.timezone = evt;
                        this.updateLabel();
                        this.inputTimezone.setValue(this.timezone);
                        this.refresh().then();
                    });
                }
                async ngOnInit() {
                    this.inputTimezone.valueChanges.forEach((value) => this.valueChanged(value)).then();
                    this.updateLabel();
                    this.initialized = true;
                }
                updateLabel() {
                    if (lodash_1.isEmpty(this.timezone) || this.timezone === 'local') {
                        this.label = `(local - Currently displayed in ${this.currentTimezone})`;
                    }
                    else {
                        this.label = 'Select Timezone';
                    }
                }
                _filter(value) {
                    const filterValue = value.toLowerCase();
                    return this.timezoneList.filter(timezone => timezone.toLowerCase().includes(filterValue));
                }
                fetchData() {
                    if (this.fetched) {
                        return this.fetched;
                    }
                    return this.fetched = this.registry.fetch(stratus_1.Stratus.Select(this.elementRef.nativeElement), this);
                }
                dataDefer(subscriber) {
                    this.subscriber = this.subscriber || subscriber;
                    if (!this.subscriber) {
                        return;
                    }
                    const dataString = this.dataRef();
                    this.subscriber.next(dataString);
                }
                dataRef() {
                    if (!this.model) {
                        return '';
                    }
                    return this.model.get(this.property);
                }
                valueChanged(value) {
                    if (!value) {
                        return;
                    }
                    this.timezone = value;
                    this.updateLabel();
                    this.inputTimezone.setValue(this.timezone);
                    if (!this.model || !this.model.completed) {
                        return;
                    }
                    this.model.set(this.property, this.timezone);
                }
                handleObservableError(err) {
                    console.error(err);
                    toastify_js_1.default({
                        text: err.toString(),
                        duration: 3000,
                        close: true,
                        stopOnFocus: true,
                        style: {
                            background: '#E14D45',
                        }
                    }).showToast();
                    return err;
                }
                timezoneSelected(value) {
                    if (!value) {
                        return;
                    }
                    this.valueChanged(value);
                }
            };
            __decorate([
                core_1.Input(),
                __metadata("design:type", String)
            ], TimezoneSelectorComponent.prototype, "target", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", String)
            ], TimezoneSelectorComponent.prototype, "targetSuffix", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Number)
            ], TimezoneSelectorComponent.prototype, "id", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Boolean)
            ], TimezoneSelectorComponent.prototype, "manifest", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Boolean)
            ], TimezoneSelectorComponent.prototype, "decouple", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Boolean)
            ], TimezoneSelectorComponent.prototype, "direct", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Object)
            ], TimezoneSelectorComponent.prototype, "api", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", String)
            ], TimezoneSelectorComponent.prototype, "urlRoot", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", String)
            ], TimezoneSelectorComponent.prototype, "type", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", String)
            ], TimezoneSelectorComponent.prototype, "property", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", String)
            ], TimezoneSelectorComponent.prototype, "endpoint", void 0);
            TimezoneSelectorComponent = __decorate([
                core_1.Component({
                    selector: `sa-internal-${moduleName}`,
                    templateUrl: `${localDir}/${moduleName}/${moduleName}.component${min}.html`,
                    changeDetection: core_1.ChangeDetectionStrategy.OnPush
                }),
                __metadata("design:paramtypes", [core_1.ElementRef,
                    core_1.ChangeDetectorRef,
                    platform_browser_1.DomSanitizer])
            ], TimezoneSelectorComponent);
            exports_1("TimezoneSelectorComponent", TimezoneSelectorComponent);
        }
    };
});

//# sourceMappingURL=timezone-selector.component.js.map
