import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../../../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_acl_index_mjs__ from "../../../built-in/acl/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_dynamic_page_utils_mjs__ from "../../../built-in/dynamic-page/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__icon_index_mjs__ from "../../../icon/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_index_mjs__ from "../../common/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useProps_mjs__ from "../../hooks/useProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Area_mjs__ from "./Action.Area.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Container_mjs__ from "./Action.Container.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Designer_mjs__ from "./Action.Designer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Drawer_mjs__ from "./Action.Drawer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Link_mjs__ from "./Action.Link.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Modal_mjs__ from "./Action.Modal.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_Page_mjs__ from "./Action.Page.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Action_style_mjs__ from "./Action.style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_mjs__ from "./hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useGetAriaLabelOfAction_mjs__ from "./hooks/useGetAriaLabelOfAction.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "./utils.mjs";
const Action = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _field_data, _field_data1;
    const { isShow = true, popover, confirm, openMode: om, containerRefKey, component, useAction = __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useA, className, icon, title, onClick, style, openSize: os, disabled: propsDisabled, actionCallback, addChild, ...others } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useProps_mjs__.useProps)(props);
    const aclCtx = (0, __WEBPACK_EXTERNAL_MODULE__built_in_acl_index_mjs__.useACLActionParamsContext)();
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useNavigate)();
    const { wrapSSR, componentCls, hashId } = (0, __WEBPACK_EXTERNAL_MODULE__Action_style_mjs__["default"])();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [formValueChanged, setFormValueChanged] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const Designer = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesigner)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const app = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
    const pageMode = app.usePageMode();
    const { run, element } = useAction(actionCallback);
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionRecordData)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollection)();
    const designerProps = fieldSchema['x-designer-props'];
    const openMode = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['openMode'];
    const openSize = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1['openSize'];
    const disabled = form.disabled || field.disabled || (null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.disabled) || propsDisabled;
    const linkageRules = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(null == fieldSchema ? void 0 : fieldSchema['x-linkage-rules']) || [], [
        fieldSchema
    ]);
    const { designable } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDesignable)();
    const tarComponent = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useComponent)(component) || component;
    const { modal } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)({
        currentForm: {
            values: record
        }
    });
    const { getAriaLabel } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useGetAriaLabelOfAction_mjs__.useGetAriaLabelOfAction)(title);
    let actionTitle = title || compile(fieldSchema.title);
    actionTitle = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].isString(actionTitle) ? t(actionTitle) : actionTitle;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        field.stateOfLinkageRules = {};
        linkageRules.filter((k)=>!k.disabled).forEach((v)=>{
            var _v_actions;
            null === (_v_actions = v.actions) || void 0 === _v_actions || _v_actions.forEach((h)=>{
                (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.linkageAction)({
                    operator: h.operator,
                    field,
                    condition: v.condition,
                    variables,
                    localVariables
                });
            });
        });
    }, [
        field,
        linkageRules,
        localVariables,
        record,
        variables
    ]);
    const handleButtonClick = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((e)=>{
        if ((0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.isPortalInBody)(e.target)) return;
        e.preventDefault();
        e.stopPropagation();
        if (!disabled && aclCtx) {
            const onOk = ()=>{
                null == onClick || onClick(e);
                const containerSchema = fieldSchema.reduceProperties((buf, s)=>'Action.Container' === s['x-component'] ? s : buf);
                if ((null == pageMode ? void 0 : pageMode.enable) && containerSchema) {
                    const target = __WEBPACK_EXTERNAL_MODULE__built_in_dynamic_page_utils_mjs__.PathHandler.getInstance().toWildcardPath({
                        collection: collection.name,
                        filterByTk: record[collection.getPrimaryKey()]
                    });
                    navigate('../' + containerSchema['x-uid'] + '/' + target);
                } else setVisible(true);
                run();
            };
            if (null == confirm ? void 0 : confirm.content) modal.confirm({
                title: t(confirm.title, {
                    title: actionTitle
                }),
                content: t(confirm.content, {
                    title: actionTitle
                }),
                onOk
            });
            else onOk();
        }
    }, [
        confirm,
        disabled,
        modal,
        onClick,
        run
    ]);
    const buttonStyle = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _field_data;
        return {
            ...style,
            opacity: designable && ((null == field ? void 0 : null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.hidden) || !aclCtx) && 0.1
        };
    }, [
        designable,
        null == field ? void 0 : null === (_field_data1 = field.data) || void 0 === _field_data1 ? void 0 : _field_data1.hidden,
        style
    ]);
    const renderButton = ()=>{
        var _field_data, _field_data1;
        if (!designable && ((null == field ? void 0 : null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.hidden) || !aclCtx)) return null;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__common_index_mjs__.SortableItem, {
            role: "button",
            "aria-label": getAriaLabel(),
            ...others,
            loading: null == field ? void 0 : null === (_field_data1 = field.data) || void 0 === _field_data1 ? void 0 : _field_data1.loading,
            icon: icon ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__icon_index_mjs__.Icon, {
                type: icon
            }) : null,
            disabled: disabled,
            style: buttonStyle,
            onClick: handleButtonClick,
            component: tarComponent || __WEBPACK_EXTERNAL_MODULE_antd__.Button,
            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])(componentCls, hashId, className, 'tb-action'),
            type: 'danger' === props.type ? void 0 : props.type,
            children: [
                actionTitle,
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Designer, {
                    ...designerProps
                })
            ]
        });
    };
    const result = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.ActionContextProvider, {
        button: renderButton(),
        visible: visible,
        setVisible: setVisible,
        formValueChanged: formValueChanged,
        setFormValueChanged: setFormValueChanged,
        openMode: openMode,
        openSize: openSize,
        containerRefKey: containerRefKey,
        fieldSchema: fieldSchema,
        children: [
            popover && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                basePath: field.address,
                onlyRenderProperties: true,
                schema: fieldSchema
            }),
            !popover && renderButton(),
            !popover && props.children,
            element
        ]
    });
    if (!isShow) return null;
    if (addChild) return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
        record: null,
        parent: record,
        children: result
    }));
    return wrapSSR(result);
}), {
    displayName: 'Action'
});
Action.Popover = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { button, visible, setVisible } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.StablePopover, {
        ...props,
        destroyTooltipOnHide: true,
        open: visible,
        onOpenChange: (visible)=>{
            setVisible(visible);
        },
        content: props.children,
        children: button
    });
}, {
    displayName: 'Action.Popover'
});
Action.Popover.Footer = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        style: {
            display: 'flex',
            justifyContent: 'flex-end',
            width: '100%'
        },
        children: props.children
    }), {
    displayName: 'Action.Popover.Footer'
});
Action.Link = __WEBPACK_EXTERNAL_MODULE__Action_Link_mjs__.ActionLink;
Action.Designer = __WEBPACK_EXTERNAL_MODULE__Action_Designer_mjs__.ActionDesigner;
Action.Drawer = __WEBPACK_EXTERNAL_MODULE__Action_Drawer_mjs__.ActionDrawer;
Action.Modal = __WEBPACK_EXTERNAL_MODULE__Action_Modal_mjs__.ActionModal;
Action.Container = __WEBPACK_EXTERNAL_MODULE__Action_Container_mjs__["default"];
Action.Page = __WEBPACK_EXTERNAL_MODULE__Action_Page_mjs__.ActionPage;
Action.Area = __WEBPACK_EXTERNAL_MODULE__Action_Area_mjs__.ActionArea;
const Action_rslib_entry_ = Action;
export { Action, Action_rslib_entry_ as default };
