import { jsx } from "react/jsx-runtime";
import "react";
import { observer } from "@tachybase/schema";
import { Switch, message } from "antd";
import { createStyles } from "antd-style";
import { useTranslation } from "react-i18next";
import { useAPIClient } from "../../../api-client/index.mjs";
import { useCollectionRecordData } from "../../../data-source/collection-record/CollectionRecordProvider.mjs";
import { useDataBlockRequest } from "../../../data-source/data-block/DataBlockRequestProvider.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-switch-checked {\n      background-color: #52c41a !important;\n    }\n    min-height: 16px;\n    height: 16px;\n    line-height: 16px;\n  "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        switchClass: css(_templateObject())
    };
});
const EnabledToggle = observer(function() {
    let props = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    const { styles } = useStyles();
    const record = useCollectionRecordData();
    const api = useAPIClient();
    const { refresh } = useDataBlockRequest();
    const { t } = useTranslation();
    const fieldName = props.fieldName || 'enabled';
    var _record_fieldName;
    const enabled = null != (_record_fieldName = null == record ? void 0 : record[fieldName]) ? _record_fieldName : false;
    const resourceName = props.resource || 'workflows';
    const checkedChildren = props.checkedChildren || t('On');
    const unCheckedChildren = props.unCheckedChildren || t('Off');
    const handleChange = async (checked)=>{
        if (props.disabled) return;
        try {
            await api.resource(resourceName).update({
                filterByTk: record.id,
                values: {
                    [fieldName]: checked
                }
            });
            message.success(t('Operation succeeded'));
            refresh();
        } catch (error) {
            message.error(t('Operation failed'));
            console.error(error);
        }
    };
    return /*#__PURE__*/ jsx(Switch, {
        checked: enabled,
        onChange: handleChange,
        checkedChildren: checkedChildren,
        unCheckedChildren: unCheckedChildren,
        size: "small",
        className: styles.switchClass,
        disabled: props.disabled
    });
});
export { EnabledToggle };
