import { b as __awaiter, c as __generator } from './index.js';
import { mchcEvent, getSymbolFromDynamicScript } from '@lm_fe/env';
import { mchcModal__, config_table_fd } from '@lm_fe/pages';
import { SMchc_TableConfig, ModelService } from '@lm_fe/service';
import { get_global_happy_arg, request } from '@lm_fe/utils';
import { useRef, useState, useEffect } from 'react';
function getConfigOne(props) {
  var _a;
  var id = (_a = get_global_happy_arg('usr1')) !== null && _a !== void 0 ? _a : props.configId;
  return request.get("/api/tableConfig/".concat(id), {
    showMsg: false
  }).then(function (r) {
    return r.data;
  });
}
function useConfigHook(props) {
  var model = useRef();
  var _a = useState(),
    config = _a[0],
    setconfig = _a[1];
  var config_raw = useRef();
  var _requesting = useRef(false);
  var _b = useState(false),
    loading = _b[0],
    setLoading = _b[1];
  useEffect(function () {
    fetch_config();
  }, [props]);
  function fetch_config() {
    if (!_requesting.current) {
      _requesting.current = true;
      setLoading(true);
      getConfigOne(props).then(function (c) {
        config_raw.current = c;
        var config = SMchc_TableConfig.process_remote(c, props);
        setconfig(config);
      })["finally"](function () {
        _requesting.current = false;
        setLoading(false);
      });
    }
  }
  function edit_config(edit_config) {
    if (edit_config === void 0) {
      edit_config = config_raw.current;
    }
    mchcModal__.open('modal_form', {
      width: '90vw',
      maskClosable: false,
      bodyStyle: {
        height: '80vh'
      },
      modal_data: {
        getInitialData: function getInitialData() {
          return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
              return [2, edit_config];
            });
          });
        },
        onSubmit: function onSubmit(v) {
          return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
              switch (_a.label) {
                case 0:
                  if (!v.id) return [3, 2];
                  return [4, SMchc_TableConfig.put(v, {
                    ignore_usr: true
                  })];
                case 1:
                  _a.sent();
                  return [3, 4];
                case 2:
                  return [4, SMchc_TableConfig.post(v, {
                    ignore_usr: true
                  })];
                case 3:
                  _a.sent();
                  _a.label = 4;
                case 4:
                  return [4, fetch_config()];
                case 5:
                  _a.sent();
                  return [2, 1];
              }
            });
          });
        },
        formDescriptions: config_table_fd
      }
    });
  }
  useEffect(function () {
    var _a;
    var watchScript = config === null || config === void 0 ? void 0 : config.watchScript;
    var rm = mchcEvent.on_rm('my_form', (_a = getSymbolFromDynamicScript(watchScript, props)) !== null && _a !== void 0 ? _a : function (event) {});
    model.current = model.current || getM(config);
    return rm;
  }, [config]);
  return [config, model, edit_config, loading];
}
function getM(config) {
  if (!(config === null || config === void 0 ? void 0 : config.name)) return;
  return new ModelService({
    n: config.name,
    needTransferParams: false,
    apiPrefix: config.apiPrefix
  });
}
function getConfigFullUrl(config) {
  if (!config) return '';
  return "".concat(config.apiPrefix).concat(config.name);
}
export { getConfigFullUrl as g, useConfigHook as u };
//# sourceMappingURL=utils.js.map
