var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/dataSourceComponents/dataSourceShow/utils.tsx
var utils_exports = {};
__export(utils_exports, {
  default: () => utils_default
});
module.exports = __toCommonJS(utils_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_ahooks = require("ahooks");
var import_utils2 = require("../provider/variables/utils");
var import_useVariables = __toESM(require("../hooks/useVariables"));
var parseValueToVariableKey = (value) => {
  if ((0, import_utils.isArr)(value) && value.length === 1 && (0, import_utils.isString)(value[0])) {
    const key = (0, import_utils2.getVariableKey)(value[0]);
    return key ? key.split(".")[0] : null;
  }
  if ((0, import_utils.isString)(value)) {
    const key = (0, import_utils2.getVariableKey)(value);
    return key ? key.split(".")[0] : null;
  }
  return null;
};
var withParseVariables = (Component, keys) => {
  return (props) => {
    const [variables, setVariables] = (0, import_react.useState)({});
    const { parseVariable, subscribeVariables } = (0, import_useVariables.default)();
    const valuesRef = (0, import_react.useRef)(/* @__PURE__ */ new Set());
    const handleVariableChange = (0, import_ahooks.useMemoizedFn)(
      (allVariables, updateVariables) => {
        const changedKey = Object.keys(updateVariables)[0];
        if (valuesRef.current.has(changedKey)) {
          setVariables((prev) => ({ ...prev, ...allVariables }));
        }
      }
    );
    (0, import_react.useEffect)(() => {
      const values = /* @__PURE__ */ new Set();
      keys.forEach((key) => {
        const variableKey = parseValueToVariableKey(props[key]);
        if (variableKey) {
          values.add(variableKey);
        }
      });
      valuesRef.current = values;
    }, [props, keys]);
    (0, import_react.useEffect)(() => {
      const unsubscribe = subscribeVariables == null ? void 0 : subscribeVariables(handleVariableChange);
      return () => {
        unsubscribe == null ? void 0 : unsubscribe();
      };
    }, []);
    const parsedProps = (0, import_react.useMemo)(() => {
      const result = { ...props };
      keys.forEach((key) => {
        const value = props[key];
        if ((0, import_utils.isArr)(value) && value.length === 1 && (0, import_utils.isString)(value[0])) {
          result[key] = parseVariable == null ? void 0 : parseVariable(value[0], variables);
        } else if ((0, import_utils.isString)(value)) {
          result[key] = parseVariable == null ? void 0 : parseVariable(value, variables);
        }
      });
      return result;
    }, [props, variables, keys]);
    return /* @__PURE__ */ import_react.default.createElement(Component, { ...parsedProps });
  };
};
var utils_default = withParseVariables;
