"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    EnabledToggle: ()=>EnabledToggle
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../api-client/index.js");
const CollectionRecordProvider_js_namespaceObject = require("../../../data-source/collection-record/CollectionRecordProvider.js");
const DataBlockRequestProvider_js_namespaceObject = require("../../../data-source/data-block/DataBlockRequestProvider.js");
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 = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        switchClass: css(_templateObject())
    };
});
const EnabledToggle = (0, schema_namespaceObject.observer)(function() {
    let props = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    const { styles } = useStyles();
    const record = (0, CollectionRecordProvider_js_namespaceObject.useCollectionRecordData)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const { refresh } = (0, DataBlockRequestProvider_js_namespaceObject.useDataBlockRequest)();
    const { t } = (0, external_react_i18next_namespaceObject.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
                }
            });
            external_antd_namespaceObject.message.success(t('Operation succeeded'));
            refresh();
        } catch (error) {
            external_antd_namespaceObject.message.error(t('Operation failed'));
            console.error(error);
        }
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Switch, {
        checked: enabled,
        onChange: handleChange,
        checkedChildren: checkedChildren,
        unCheckedChildren: unCheckedChildren,
        size: "small",
        className: styles.switchClass,
        disabled: props.disabled
    });
});
exports.EnabledToggle = __webpack_exports__.EnabledToggle;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "EnabledToggle"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
