import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import styled from "styled-components";
import { BaseLogicExpression } from "./BaseLogic";
import { ExpressionPiece } from "../../ExpressionStyles";
import { ObserveState } from "../../../CommonUI/StateManagement/ObserveState";
import { StateManager } from "../../../CommonUI/StateManagement/StateManager";
import { DataUtilities } from "../../../Common/DataUtilities";
import { ExpressionValue } from "../../ExpressionValue";
import { ValidationIssue, ValidationLevel } from "../../../Common/IDesignValidationProvider";
import VariablePicker from "../../../CommonUI/VariablePicker";
import { AttentionIcon } from "../../../CommonUI/Icon/AttentionIcon";
import { XDataDefinition } from "../../../Common/Data/XDataDefinition";
export class ExistsExpression extends BaseLogicExpression {
    constructor() {
        super(...arguments);
        this.__type = "Exists";
    }
    render() {
        var _a, _b;
        let dataDef = ((_b = (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.definition) === null || _b === void 0 ? void 0 : _b.getInputDefinition()) || new XDataDefinition();
        return (_jsx(ObserveState, { listenTo: this, control: () => {
                let validationIssues = this.getValidationIssues();
                return (_jsxs(ExpressionPiece, { children: ["exists", _jsxs(VariablePickerWrapper, Object.assign({ error: validationIssues.length > 0 }, { children: [dataDef && (_jsx(ObserveState, { listenTo: dataDef, control: () => {
                                        var _a, _b;
                                        return (_jsx(VariablePicker, { selected: this.dataPath, data: dataDef, allowNewVariable: (_b = (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.definition) === null || _b === void 0 ? void 0 : _b.canChangeInputs, onVariableSet: (path, def) => {
                                                if (def) {
                                                    dataDef.fields.push(def);
                                                    this.dataPath = def.name;
                                                }
                                                else {
                                                    this.dataPath = path;
                                                }
                                                StateManager.propertyChanged(this, "dataPath");
                                            } }, void 0));
                                    } }, void 0)),
                                validationIssues.length > 0 && (_jsxs(_Fragment, { children: [_jsx(ValidationErrorRender, { children: _jsx(AttentionIcon, {}, void 0) }, void 0),
                                        _jsx(ErrorMessageRender, Object.assign({ className: "ExpressionValidation-errorMessage", type: validationIssues[0].level }, { children: validationIssues[0].issue }), void 0)] }, void 0))] }), void 0)] }, void 0));
            } }, void 0));
    }
    evaluate(data, testContext) {
        var _a, _b, _c, _d, _e, _f, _g;
        if (!this.dataPath) {
            throw new Error("dataPath is not set");
        }
        let field = ((_c = (_b = (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.definition) === null || _b === void 0 ? void 0 : _b.requiredVariable) === null || _c === void 0 ? void 0 : _c.name) === this.dataPath
            ? (_e = (_d = this.editContext) === null || _d === void 0 ? void 0 : _d.definition) === null || _e === void 0 ? void 0 : _e.requiredVariable : undefined;
        if (!field) {
            let dataDefinition = (_g = (_f = this.editContext) === null || _f === void 0 ? void 0 : _f.definition) === null || _g === void 0 ? void 0 : _g.getInputDefinition();
            if (!dataDefinition) {
                throw new Error("can not get the data definition as edit context or expression definition is not set");
            }
            field = DataUtilities.getFieldFromDataDefinitionByProperty(dataDefinition.fields, this.dataPath, "name");
        }
        let value = new ExpressionValue();
        value.expectedType = field;
        value.variableName = this.dataPath;
        let result = value.evaluate(data, testContext);
        if (field.list) {
            if (Array.isArray(result)) {
                return result.length > 0;
            }
        }
        else {
            return result !== null && result !== undefined;
        }
    }
    getStorageData() {
        let data = super.getStorageData();
        Reflect.set(data, "dataPath", this.dataPath);
        return data;
    }
    setStorageData(data) {
        super.setStorageData(data);
        Reflect.set(this, "dataPath", Reflect.get(data, "dataPath"));
    }
    getValidationIssues() {
        var _a, _b, _c, _d, _e, _f;
        if (!this.dataPath) {
            return [new ValidationIssue("data path is not set")];
        }
        if (((_b = (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.definition) === null || _b === void 0 ? void 0 : _b.requiredVariable) &&
            ((_d = (_c = this.editContext) === null || _c === void 0 ? void 0 : _c.definition) === null || _d === void 0 ? void 0 : _d.requiredVariable.name) === this.dataPath) {
            return [];
        }
        let dataDefinition = (_f = (_e = this.editContext) === null || _e === void 0 ? void 0 : _e.definition) === null || _f === void 0 ? void 0 : _f.getInputDefinition();
        if (!dataDefinition) {
            return [
                new ValidationIssue("can not get the data definition as edit context or expression definition is not set"),
            ];
        }
        try {
            let field = DataUtilities.getFieldFromDataDefinitionByProperty(dataDefinition.fields, this.dataPath, "name");
            if (!field) {
                return [new ValidationIssue("invalid data path")];
            }
            return [];
        }
        catch (e) {
            return [new ValidationIssue("invalid data path")];
        }
    }
}
const VariablePickerWrapper = styled.div `
    width: 100px;
    padding: 5px;

    position: relative;

    .VariablePicker_Content {
        background: #fff;

        min-width: 200px;
    }

    border: solid 2px transparent;

    border-radius: 8px;

    ${(props) => props.error
    ? `
                
                .ExpressionValidation-errorMessage {
                    opacity: 0;
                    z-index: -1;
                }

                &:hover {
                    border: solid 2px #ee3e54;
                    .ExpressionValidation-errorMessage {
                        opacity: 1;
                        z-index: 1;
                    }
                }
            `
    : ``}
`;
const ErrorMessageRender = styled.div `
    font-family: Inter;
    font-size: 8px;
    font-style: normal;
    font-weight: 500;
    line-height: 10px;
    letter-spacing: 0.1px;
    text-align: left;

    white-space: pre;

    z-index: 2;

    position: absolute;
    top: calc(100% + 3px);
    left: 0;
    color: ${(props) => (props.type === ValidationLevel.ERROR ? "#ee3e54" : "#F4955F")};
`;
const ValidationErrorRender = styled.div `
    position: absolute;
    top: -5px;
    right: 0px;
`;
//# sourceMappingURL=Exists.js.map