import { jsx, jsxs } from "react/jsx-runtime";
import react, { useCallback, useContext, useMemo, useState } from "react";
import { observer, uid } from "@tachybase/schema";
import { CloseCircleOutlined } from "@ant-design/icons";
import { TreeSelect } from "@tego/client";
import { Select, Space } from "antd";
import { createStyles } from "antd-style";
import { useTranslation } from "react-i18next";
import { useCompile } from "../../index.mjs";
import { LinkageLogicContext, RemoveActionContext } from "./context.mjs";
import { DynamicComponent } from "./DynamicComponent.mjs";
import { ActionType } from "./type.mjs";
import { useValues } from "./useValues.mjs";
import { ValueDynamicComponent } from "./ValueDynamicComponent.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      .ant-space {\n        display: inline-block;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      .ant-space {\n        display: inline-block;\n      }\n      .ant-space-item {\n        max-width: 95%;\n        display: inline-block;\n        margin: 2px;\n        vertical-align: top;\n      }\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        "\n      min-width: 120px;\n    "
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
function _templateObject3() {
    const data = _tagged_template_literal([
        "\n      min-width: 160px;\n    "
    ]);
    _templateObject3 = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        container: css(_templateObject()),
        item: css(_templateObject1()),
        select: css(_templateObject2()),
        tree: css(_templateObject3())
    };
});
const FormFieldLinkageRuleAction = observer((props)=>{
    const { value, options, collectionName } = props;
    const { styles } = useStyles();
    const { t } = useTranslation();
    const compile = useCompile();
    const remove = useContext(RemoveActionContext);
    const { schema, fields, operator, setDataIndex, setOperator, setValue, value: fieldValue, operators } = useValues(options);
    return /*#__PURE__*/ jsx(LinkageLogicContext.Provider, {
        value: uid(),
        children: /*#__PURE__*/ jsx("div", {
            style: {
                marginBottom: 8
            },
            className: styles.container,
            children: /*#__PURE__*/ jsxs(Space, {
                className: styles.item,
                children: [
                    /*#__PURE__*/ jsx(TreeSelect, {
                        role: "button",
                        "data-testid": "select-linkage-property-field",
                        className: styles.tree,
                        fieldNames: {
                            label: 'title',
                            value: 'name',
                            children: 'children'
                        },
                        treeCheckable: true,
                        value: null == value ? void 0 : value.targetFields,
                        multiple: true,
                        allowClear: true,
                        treeData: compile(fields),
                        onChange: (value)=>{
                            setDataIndex(value);
                        },
                        placeholder: t('Select field')
                    }),
                    /*#__PURE__*/ jsx(Select, {
                        role: "button",
                        "data-testid": "select-linkage-action-field",
                        popupMatchSelectWidth: false,
                        value: operator,
                        className: styles.select,
                        options: compile(operators),
                        onChange: (value)=>{
                            setOperator(value);
                        },
                        placeholder: t('action')
                    }),
                    [
                        ActionType.Value
                    ].includes(operator) && /*#__PURE__*/ jsx(ValueDynamicComponent, {
                        fieldValue: fieldValue,
                        schema: schema,
                        setValue: setValue,
                        collectionName: collectionName
                    }),
                    !props.disabled && /*#__PURE__*/ jsx("a", {
                        role: "button",
                        "aria-label": "icon-close",
                        children: /*#__PURE__*/ jsx(CloseCircleOutlined, {
                            onClick: ()=>remove(),
                            style: {
                                color: '#bfbfbf'
                            }
                        })
                    })
                ]
            })
        })
    });
}, {
    displayName: 'FormFieldLinkageRuleAction'
});
const FormButtonLinkageRuleAction = observer((props)=>{
    const { value, options } = props;
    const { t } = useTranslation();
    const compile = useCompile();
    const [editFlag, setEditFlag] = useState(false);
    const remove = useContext(RemoveActionContext);
    const { schema, operator, setOperator, setValue } = useValues(options);
    const operators = useMemo(()=>compile([
            {
                label: t('Visible'),
                value: ActionType.Visible,
                schema: {}
            },
            {
                label: t('Hidden'),
                value: ActionType.Hidden,
                schema: {}
            },
            {
                label: t('Disabled'),
                value: ActionType.Disabled,
                schema: {}
            },
            {
                label: t('Enabled'),
                value: ActionType.Active,
                schema: {}
            }
        ]), [
        compile,
        t
    ]);
    const onChange = useCallback((value)=>{
        const flag = [
            ActionType.Value
        ].includes(value);
        setEditFlag(flag);
        setOperator(value);
    }, [
        setOperator
    ]);
    const onChangeValue = useCallback((value)=>{
        setValue(value);
    }, [
        setValue
    ]);
    const closeStyle = useMemo(()=>({
            color: '#bfbfbf'
        }), []);
    return /*#__PURE__*/ jsx("div", {
        style: {
            marginBottom: 8
        },
        children: /*#__PURE__*/ jsxs(Space, {
            children: [
                /*#__PURE__*/ jsx(Select, {
                    "data-testid": "select-linkage-properties",
                    popupMatchSelectWidth: false,
                    value: operator,
                    options: operators,
                    onChange: onChange,
                    placeholder: t('action')
                }),
                editFlag && /*#__PURE__*/ react.createElement(DynamicComponent, {
                    value,
                    schema,
                    onChange: onChangeValue
                }),
                !props.disabled && /*#__PURE__*/ jsx("a", {
                    role: "button",
                    "aria-label": "icon-close",
                    children: /*#__PURE__*/ jsx(CloseCircleOutlined, {
                        onClick: remove,
                        style: closeStyle
                    })
                })
            ]
        })
    });
}, {
    displayName: 'FormButtonLinkageRuleAction'
});
export { FormButtonLinkageRuleAction, FormFieldLinkageRuleAction };
