import { ElLoading } from 'element-plus';
import { isRef, unref } from 'vue';

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
const DEFAULT_CONFIG = {
  lock: true,
  text: "",
  //background: 'rgba(0, 199, 255, 0.3)',
  background: "rgba(94, 158, 214, 0.12)",
  // 景德镇青花色
  //spinner: 'circle-check'
  //spinner: 'el-icon-potato',
  spinner: "el-icon-loading"
};
class FlowLoadingManager {
  constructor() {
    __publicField(this, "instance", null);
  }
  // 2. 统一返回类型
  start(options) {
    try {
      if (this.instance) {
        console.warn("Existing loading instance detected");
        return this.createController();
      }
      const resolved = this.normalizeOptions(options);
      this.validateConfig(resolved);
      this.instance = ElLoading.service(resolved);
      return this.createController();
    } catch (e) {
      console.error("Loading initialization failed:", e);
      return this.createController(true);
    }
  }
  normalizeOptions(options) {
    const baseConfig = { ...DEFAULT_CONFIG };
    if (!options) return baseConfig;
    if (typeof options === "string" || isRef(options)) {
      return {
        ...baseConfig,
        text: unref(options)
      };
    }
    return {
      ...baseConfig,
      ...options,
      text: options.text ? unref(options.text) : baseConfig.text
    };
  }
  validateConfig(config) {
    if (config.spinner && typeof config.spinner !== "string") {
      console.warn("Custom spinner components require proper registration");
    }
    if (typeof config.text !== "string") {
      throw new Error("Loading text must be a string");
    }
  }
  // 3. 增强控制器创建方法
  createController(isError = false) {
    return {
      step: (text) => {
        if (!isError && this.instance) {
          this.instance.setText(unref(text));
        }
        return this.createController(isError);
      },
      end: () => {
        var _a;
        if (!isError) {
          (_a = this.instance) == null ? void 0 : _a.close();
        }
        this.instance = null;
      }
    };
  }
}
const flowLoading = new FlowLoadingManager();

export { flowLoading };
