import { jsx, jsxs } from "react/jsx-runtime";
import { useEffect } from "react";
import { useField } from "@tachybase/schema";
import { EditOutlined, RightSquareOutlined } from "@ant-design/icons";
import { Button } from "antd";
import { createStyles } from "antd-style";
import { useTranslation } from "react-i18next";
import { useAsyncData } from "../../../../async-data-provider/index.mjs";
import { Input } from "../../../../schema-component/index.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      position: relative;\n      .ant-input {\n        width: 100%;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        input: css(_templateObject())
    };
});
const SQLInput = (param)=>{
    let { disabled } = param;
    const { t } = useTranslation();
    const { run, loading, error } = useAsyncData();
    const { styles } = useStyles();
    const field = useField();
    const execute = ()=>{
        if (!field.value) return;
        run(field.value);
    };
    const toggleEditing = ()=>{
        if (!disabled && !field.value) return;
        if (!disabled) run(field.value);
        field.setComponentProps({
            disabled: !disabled
        });
    };
    useEffect(()=>{
        if (error) field.setComponentProps({
            disabled: false
        });
    }, [
        field,
        error
    ]);
    return /*#__PURE__*/ jsxs("div", {
        className: styles.input,
        children: [
            /*#__PURE__*/ jsx(Input.TextArea, {
                value: field.value,
                disabled: disabled,
                onChange: (e)=>field.value = e.target.value
            }),
            /*#__PURE__*/ jsxs(Button.Group, {
                children: [
                    /*#__PURE__*/ jsx(Button, {
                        onClick: toggleEditing,
                        ghost: true,
                        size: "small",
                        type: "primary",
                        icon: /*#__PURE__*/ jsx(EditOutlined, {}),
                        children: t(disabled ? 'Edit' : 'Confirm')
                    }),
                    /*#__PURE__*/ jsx(Button, {
                        onClick: ()=>execute(),
                        loading: loading,
                        ghost: true,
                        size: "small",
                        type: "primary",
                        icon: /*#__PURE__*/ jsx(RightSquareOutlined, {}),
                        children: t('Execute')
                    })
                ]
            })
        ]
    });
};
export { SQLInput };
