import * as i3 from '@acorex/components/button';
import { AXButtonModule } from '@acorex/components/button';
import * as i2 from '@acorex/components/decorators';
import { AXDecoratorModule } from '@acorex/components/decorators';
import { AXDialogModule } from '@acorex/components/dialog';
import * as i6 from '@acorex/components/dropdown-button';
import { AXDropdownButtonModule } from '@acorex/components/dropdown-button';
import * as i1 from '@acorex/components/form';
import { AXFormModule } from '@acorex/components/form';
import * as i5 from '@acorex/components/label';
import { AXLabelModule } from '@acorex/components/label';
import * as i4 from '@acorex/components/loading';
import { AXLoadingModule } from '@acorex/components/loading';
import { AXBasePageComponent } from '@acorex/components/page';
import { AXTabsModule } from '@acorex/components/tabs';
import { AXTooltipModule } from '@acorex/components/tooltip';
import * as i1$2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { viewChild, inject, signal, effect, ChangeDetectionStrategy, Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AXValidationModule } from '@acorex/core/validation';
import { AXDropdownModule } from '@acorex/components/dropdown';
import { AXToastService } from '@acorex/components/toast';
import * as i10 from '@acorex/core/translation';
import { AXTranslationService, AXTranslationModule } from '@acorex/core/translation';
import { AXPExpressionEvaluatorService, AXPGridLayoutDirective } from '@acorex/platform/core';
import * as i1$1 from '@acorex/platform/layout/components';
import { AXPComponentSlotModule } from '@acorex/platform/layout/components';
import * as i7 from '@acorex/platform/layout/widget-core';
import { AXPWidgetContainerComponent, AXPWidgetCoreModule } from '@acorex/platform/layout/widget-core';
import { AXPWidgetsModule } from '@acorex/platform/layout/widgets';
import { AXPWorkflowService } from '@acorex/platform/workflow';
import { get } from 'lodash-es';

class AXPEntityMasterCreateViewComponent extends AXBasePageComponent {
    constructor() {
        super(...arguments);
        this.container = viewChild(AXPWidgetContainerComponent, ...(ngDevMode ? [{ debugName: "container" }] : []));
        this.evaluator = inject(AXPExpressionEvaluatorService);
        this.toastService = inject(AXToastService);
        this.translationService = inject(AXTranslationService);
        this.workflow = inject(AXPWorkflowService);
        this.hiddenProperties = signal([], ...(ngDevMode ? [{ debugName: "hiddenProperties" }] : []));
        this.#visibilityEffect = effect(async () => {
            const formContext = this.vm.context();
            const allElements = this.vm.sections().flatMap((section) => section.elements());
            const visibilityConfig = allElements.map((element) => ({
                name: element.name(),
                visible: element.isVisible(),
            }));
            const evaluatedElements = await this.evaluator.evaluate(visibilityConfig, {
                context: {
                    eval: (path) => get(formContext, path),
                },
            });
            const hiddenElementNames = evaluatedElements
                .filter((element) => element.visible == false)
                .map((element) => element.name);
            this.hiddenProperties.set(hiddenElementNames);
        }, ...(ngDevMode ? [{ debugName: "#visibilityEffect" }] : []));
    }
    ngAfterViewInit() {
        this.vm.builder = this.container().builderService;
    }
    #visibilityEffect;
    handleCloseClick() {
        this.close();
    }
    async handleBackClick() {
        //await this.vm.executeCommand('back');
    }
    async handleNextClick(form) {
        // const formResult = await form.validate();
        // if (formResult.result) {
        //   await this.vm.executeCommand('next');
        // }
    }
    async handleSaveClick(form) {
        const formResult = await form.validate();
        if (formResult.result) {
            try {
                const record = await this.vm.save();
                this.close({ save: true, recordId: record?.id, item: record, redirect: this.vm.redirect() });
            }
            catch (error) {
                const errorMessage = error?.message || error?.toString() || await this.translationService.translateAsync('@general:messages.generic.error.description');
                this.toastService.show({
                    color: 'danger',
                    title: await this.translationService.translateAsync('@general:messages.generic.error.title'),
                    content: errorMessage,
                    location: 'bottom-center',
                    closeButton: true,
                    timeOut: 5000,
                    timeOutProgress: true,
                });
            }
        }
    }
    async handleSaveAndNewClick(form) {
        const formResult = await form.validate();
        if (formResult.result) {
            try {
                const record = await this.vm.save();
                this.close({ save: true, recordId: record?.id, item: record, redirect: false });
                await this.vm.createNewOne();
            }
            catch (error) {
                const errorMessage = error?.message || error?.toString() || await this.translationService.translateAsync('@general:messages.generic.error.description');
                this.toastService.show({
                    color: 'danger',
                    title: await this.translationService.translateAsync('@general:messages.generic.error.title'),
                    content: errorMessage,
                    location: 'bottom-center',
                    closeButton: true,
                    timeOut: 5000,
                    timeOutProgress: true,
                });
            }
        }
    }
    async handleContextChange(e) {
        this.vm.context.set(e.data);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: AXPEntityMasterCreateViewComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.16", type: AXPEntityMasterCreateViewComponent, isStandalone: true, selector: "ng-component", viewQueries: [{ propertyName: "container", first: true, predicate: AXPWidgetContainerComponent, descendants: true, isSignal: true }], usesInheritance: true, ngImport: i0, template: "<axp-component-slot name=\"entity-create-header\" [context]=\"vm.context()\"></axp-component-slot>\n<axp-widgets-container [context]=\"vm.context()\" (onContextChanged)=\"handleContextChange($event)\">\n  <ax-form\n    id=\"axp-entity-form-create\"\n    class=\"ax-flex ax-flex-col ax-gap-6 ax-p-4 ax-pb-8\"\n    #form\n    [messageStyle]=\"'bottom'\"\n    [updateOn]=\"'blur'\"\n  >\n    <!-- Begin Sections -->\n    <div class=\"ax-grid ax-grid-cols-12 ax-gap-4\">\n      @for (section of vm.sections(); track section.name()) {\n        <div class=\"ax-col-span-12\">\n          <ax-form #form>\n            @if (vm.sections().length > 1 && !section.description()) {\n              <div class=\"ax-pb-4\">\n                <span class=\"ax-font-bold ax-text-xl\">{{ section.title() | translate | async }}</span>\n                @if (section.description()) {\n                  <p class=\"ax-text-sm ax-text-gray-500\">{{ section.description() }}</p>\n                }\n              </div>\n            }\n            <div class=\"ax-card-body !ax-grid !ax-grid-cols-12 !ax-gap-4\">\n              @for (attr of section.elements(); track $index) {\n                <ax-form-field\n                  [attr.id]=\"'axp-field-' + attr.name()\"\n                  class=\"ax-flex ax-flex-col ax-gap-1\"\n                  [axp-grid-layout]=\"attr.layout()\"\n                  [class.ax-hidden]=\"hiddenProperties().includes(attr.name())\"\n                >\n                  @if (attr.layout().label?.visible != false) {\n                    <ax-label class=\"ax-font-semibold ax-text-sm\" [required]=\"attr.isRequired()\">\n                      @if (attr.title()) {\n                        {{ attr.title() | translate | async }}\n                      } @else {\n                        &nbsp;\n                      }\n                    </ax-label>\n                  }\n                  <ng-container\n                    axp-widget-renderer\n                    [node]=\"attr.node()\"\n                    [mode]=\"'edit'\"\n                    #widget=\"widgetRenderer\"\n                  ></ng-container>\n                </ax-form-field>\n              }\n            </div>\n          </ax-form>\n        </div>\n      }\n    </div>\n    <!-- Finish Sections  -->\n  </ax-form>\n</axp-widgets-container>\n@if (slot.isEmpty()) {\n  <ax-footer id=\"axp-entity-footer-create\">\n    <ax-prefix>\n      <axp-component-slot name=\"entity-create-footer-prefix\" [context]=\"vm.context()\"></axp-component-slot>\n    </ax-prefix>\n    <ax-suffix>\n      <ax-button\n        id=\"axp-btn-cancel\"\n        look=\"solid\"\n        [color]=\"'default'\"\n        [text]=\"'@general:actions.cancel.title' | translate | async\"\n        (onClick)=\"handleCloseClick()\"\n      >\n      </ax-button>\n      @if (vm.canCreateNewOne()) {\n        <ax-dropdown-button\n          id=\"axp-btn-save\"\n          [disabled]=\"vm.isInProgress()\"\n          color=\"primary\"\n          [text]=\"'@general:actions.save.title' | translate | async\"\n          look=\"solid\"\n          (onClick)=\"handleSaveClick(form)\"\n        >\n          @if (vm.isInProgress()) {\n            <ax-loading></ax-loading>\n          }\n          <ax-button-item-list>\n            <ax-button-item\n              id=\"axp-btn-save-and-new\"\n              [text]=\"\n                ('@general:actions.save.title' | translate | async) +\n                ' ' +\n                ('@general:actions.create.title' | translate | async)\n              \"\n              (onClick)=\"handleSaveAndNewClick(form)\"\n            >\n            </ax-button-item>\n          </ax-button-item-list>\n        </ax-dropdown-button>\n      } @else {\n        <ax-button\n          id=\"axp-btn-save\"\n          look=\"solid\"\n          color=\"primary\"\n          [text]=\"'@general:actions.save.title' | translate | async\"\n          (onClick)=\"handleSaveClick(form)\"\n        >\n          @if (vm.isInProgress()) {\n            <ax-loading></ax-loading>\n          }\n        </ax-button>\n      }\n      <axp-component-slot name=\"entity-create-footer-suffix\" [context]=\"vm.context()\"></axp-component-slot>\n    </ax-suffix>\n  </ax-footer>\n}\n\n<axp-component-slot name=\"entity-create-footer\" #slot=\"slot\" [context]=\"vm.context()\"></axp-component-slot>\n", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "ngmodule", type: FormsModule }, { kind: "ngmodule", type: AXFormModule }, { kind: "component", type: i1.AXFormFieldComponent, selector: "ax-form-field", inputs: ["labelMode"] }, { kind: "component", type: i1.AXFormComponent, selector: "ax-form", inputs: ["disabled", "readonly", "labelMode", "look", "messageStyle", "updateOn", "inUserInteractionActive"], outputs: ["onValidate", "updateOnChange"] }, { kind: "ngmodule", type: AXDecoratorModule }, { kind: "component", type: i2.AXDecoratorGenericComponent, selector: "ax-footer, ax-header, ax-content, ax-divider, ax-form-hint, ax-prefix, ax-suffix, ax-text, ax-title, ax-subtitle, ax-placeholder, ax-overlay" }, { kind: "ngmodule", type: AXButtonModule }, { kind: "component", type: i3.AXButtonComponent, selector: "ax-button", inputs: ["disabled", "size", "tabIndex", "color", "look", "text", "toggleable", "selected", "iconOnly", "type", "loadingText"], outputs: ["onBlur", "onFocus", "onClick", "selectedChange", "toggleableChange", "lookChange", "colorChange", "disabledChange", "loadingTextChange"] }, { kind: "component", type: i3.AXButtonItemComponent, selector: "ax-button-item", inputs: ["color", "disabled", "text", "selected", "divided", "data", "name"], outputs: ["onClick", "onFocus", "onBlur", "disabledChange"] }, { kind: "component", type: i3.AXButtonItemListComponent, selector: "ax-button-item-list", inputs: ["items", "closeParentOnClick", "lockOnLoading"], outputs: ["onItemClick"] }, { kind: "ngmodule", type: AXDialogModule }, { kind: "ngmodule", type: AXLoadingModule }, { kind: "component", type: i4.AXLoadingComponent, selector: "ax-loading", inputs: ["visible", "type", "context"], outputs: ["visibleChange"] }, { kind: "ngmodule", type: AXTabsModule }, { kind: "ngmodule", type: AXTooltipModule }, { kind: "ngmodule", type: AXValidationModule }, { kind: "ngmodule", type: AXLabelModule }, { kind: "component", type: i5.AXLabelComponent, selector: "ax-label", inputs: ["required", "for"], outputs: ["requiredChange"] }, { kind: "ngmodule", type: AXDropdownModule }, { kind: "ngmodule", type: AXDropdownButtonModule }, { kind: "component", type: i6.AXDropdownButtonComponent, selector: "ax-dropdown-button", inputs: ["disabled", "size", "color", "look", "text", "type", "mode"], outputs: ["onBlur", "onFocus", "onClick", "selectedChange", "lookChange", "colorChange", "disabledChange"] }, { kind: "ngmodule", type: AXTranslationModule }, { kind: "ngmodule", type: 
                //
                AXPWidgetCoreModule }, { kind: "component", type: i7.AXPWidgetContainerComponent, selector: "axp-widgets-container", inputs: ["context", "functions"], outputs: ["onContextChanged"] }, { kind: "directive", type: i7.AXPWidgetRendererDirective, selector: "[axp-widget-renderer]", inputs: ["parentNode", "index", "mode", "node"], outputs: ["onOptionsChanged", "onValueChanged", "onLoad"], exportAs: ["widgetRenderer"] }, { kind: "ngmodule", type: AXPComponentSlotModule }, { kind: "directive", type: i1$1.AXPComponentSlotDirective, selector: "axp-component-slot", inputs: ["name", "host", "context"], exportAs: ["slot"] }, { kind: "ngmodule", type: AXPWidgetsModule }, { kind: "directive", type: AXPGridLayoutDirective, selector: "[axp-grid-layout]", inputs: ["axp-grid-layout"] }, { kind: "pipe", type: i1$2.AsyncPipe, name: "async" }, { kind: "pipe", type: i10.AXTranslatorPipe, name: "translate" }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: AXPEntityMasterCreateViewComponent, decorators: [{
            type: Component,
            args: [{ imports: [
                        CommonModule,
                        FormsModule,
                        AXFormModule,
                        AXDecoratorModule,
                        CommonModule,
                        AXButtonModule,
                        AXDialogModule,
                        AXLoadingModule,
                        AXTabsModule,
                        AXTooltipModule,
                        AXValidationModule,
                        AXLabelModule,
                        AXDropdownModule,
                        AXDropdownButtonModule,
                        AXTranslationModule,
                        //
                        AXPWidgetCoreModule,
                        AXPComponentSlotModule,
                        AXPWidgetsModule,
                        AXPGridLayoutDirective,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<axp-component-slot name=\"entity-create-header\" [context]=\"vm.context()\"></axp-component-slot>\n<axp-widgets-container [context]=\"vm.context()\" (onContextChanged)=\"handleContextChange($event)\">\n  <ax-form\n    id=\"axp-entity-form-create\"\n    class=\"ax-flex ax-flex-col ax-gap-6 ax-p-4 ax-pb-8\"\n    #form\n    [messageStyle]=\"'bottom'\"\n    [updateOn]=\"'blur'\"\n  >\n    <!-- Begin Sections -->\n    <div class=\"ax-grid ax-grid-cols-12 ax-gap-4\">\n      @for (section of vm.sections(); track section.name()) {\n        <div class=\"ax-col-span-12\">\n          <ax-form #form>\n            @if (vm.sections().length > 1 && !section.description()) {\n              <div class=\"ax-pb-4\">\n                <span class=\"ax-font-bold ax-text-xl\">{{ section.title() | translate | async }}</span>\n                @if (section.description()) {\n                  <p class=\"ax-text-sm ax-text-gray-500\">{{ section.description() }}</p>\n                }\n              </div>\n            }\n            <div class=\"ax-card-body !ax-grid !ax-grid-cols-12 !ax-gap-4\">\n              @for (attr of section.elements(); track $index) {\n                <ax-form-field\n                  [attr.id]=\"'axp-field-' + attr.name()\"\n                  class=\"ax-flex ax-flex-col ax-gap-1\"\n                  [axp-grid-layout]=\"attr.layout()\"\n                  [class.ax-hidden]=\"hiddenProperties().includes(attr.name())\"\n                >\n                  @if (attr.layout().label?.visible != false) {\n                    <ax-label class=\"ax-font-semibold ax-text-sm\" [required]=\"attr.isRequired()\">\n                      @if (attr.title()) {\n                        {{ attr.title() | translate | async }}\n                      } @else {\n                        &nbsp;\n                      }\n                    </ax-label>\n                  }\n                  <ng-container\n                    axp-widget-renderer\n                    [node]=\"attr.node()\"\n                    [mode]=\"'edit'\"\n                    #widget=\"widgetRenderer\"\n                  ></ng-container>\n                </ax-form-field>\n              }\n            </div>\n          </ax-form>\n        </div>\n      }\n    </div>\n    <!-- Finish Sections  -->\n  </ax-form>\n</axp-widgets-container>\n@if (slot.isEmpty()) {\n  <ax-footer id=\"axp-entity-footer-create\">\n    <ax-prefix>\n      <axp-component-slot name=\"entity-create-footer-prefix\" [context]=\"vm.context()\"></axp-component-slot>\n    </ax-prefix>\n    <ax-suffix>\n      <ax-button\n        id=\"axp-btn-cancel\"\n        look=\"solid\"\n        [color]=\"'default'\"\n        [text]=\"'@general:actions.cancel.title' | translate | async\"\n        (onClick)=\"handleCloseClick()\"\n      >\n      </ax-button>\n      @if (vm.canCreateNewOne()) {\n        <ax-dropdown-button\n          id=\"axp-btn-save\"\n          [disabled]=\"vm.isInProgress()\"\n          color=\"primary\"\n          [text]=\"'@general:actions.save.title' | translate | async\"\n          look=\"solid\"\n          (onClick)=\"handleSaveClick(form)\"\n        >\n          @if (vm.isInProgress()) {\n            <ax-loading></ax-loading>\n          }\n          <ax-button-item-list>\n            <ax-button-item\n              id=\"axp-btn-save-and-new\"\n              [text]=\"\n                ('@general:actions.save.title' | translate | async) +\n                ' ' +\n                ('@general:actions.create.title' | translate | async)\n              \"\n              (onClick)=\"handleSaveAndNewClick(form)\"\n            >\n            </ax-button-item>\n          </ax-button-item-list>\n        </ax-dropdown-button>\n      } @else {\n        <ax-button\n          id=\"axp-btn-save\"\n          look=\"solid\"\n          color=\"primary\"\n          [text]=\"'@general:actions.save.title' | translate | async\"\n          (onClick)=\"handleSaveClick(form)\"\n        >\n          @if (vm.isInProgress()) {\n            <ax-loading></ax-loading>\n          }\n        </ax-button>\n      }\n      <axp-component-slot name=\"entity-create-footer-suffix\" [context]=\"vm.context()\"></axp-component-slot>\n    </ax-suffix>\n  </ax-footer>\n}\n\n<axp-component-slot name=\"entity-create-footer\" #slot=\"slot\" [context]=\"vm.context()\"></axp-component-slot>\n" }]
        }], propDecorators: { container: [{ type: i0.ViewChild, args: [i0.forwardRef(() => AXPWidgetContainerComponent), { isSignal: true }] }] } });

export { AXPEntityMasterCreateViewComponent };
//# sourceMappingURL=acorex-platform-themes-default-entity-master-create-view.component-CJcbkSBF.mjs.map
