import {
  findWorkspaceRoot
} from "./chunk-BDW32BZK.js";
import {
  __name,
  init_esm_shims
} from "./chunk-BFNZYC5O.js";

// src/utilities/apply-workspace-tokens.ts
init_esm_shims();
var applyWorkspaceBaseTokens = /* @__PURE__ */ __name(async (option, tokenParams) => {
  let result = option;
  if (!result) {
    return result;
  }
  if (tokenParams) {
    const optionKeys = Object.keys(tokenParams);
    if (optionKeys.some((optionKey) => result.includes(`{${optionKey}}`))) {
      for (const optionKey of optionKeys) {
        if (result.includes(`{${optionKey}}`)) {
          result = result.replaceAll(`{${optionKey}}`, tokenParams?.[optionKey] || "");
        }
      }
    }
  }
  if (tokenParams.config) {
    const configKeys = Object.keys(tokenParams.config);
    if (configKeys.some((configKey) => result.includes(`{${configKey}}`))) {
      for (const configKey of configKeys) {
        if (result.includes(`{${configKey}}`)) {
          result = result.replaceAll(`{${configKey}}`, tokenParams.config[configKey] || "");
        }
      }
    }
  }
  if (result.includes("{workspaceRoot}")) {
    result = result.replaceAll("{workspaceRoot}", tokenParams.workspaceRoot ?? tokenParams.config?.workspaceRoot ?? findWorkspaceRoot());
  }
  return result;
}, "applyWorkspaceBaseTokens");
var applyWorkspaceProjectTokens = /* @__PURE__ */ __name((option, tokenParams) => {
  return applyWorkspaceBaseTokens(option, tokenParams);
}, "applyWorkspaceProjectTokens");
var applyWorkspaceTokens = /* @__PURE__ */ __name(async (options, tokenParams, tokenizerFn) => {
  if (!options) {
    return {};
  }
  const result = {};
  for (const option of Object.keys(options)) {
    if (typeof options[option] === "string") {
      result[option] = await Promise.resolve(tokenizerFn(options[option], tokenParams));
    } else if (Array.isArray(options[option])) {
      result[option] = await Promise.all(options[option].map(async (item) => typeof item === "string" ? await Promise.resolve(tokenizerFn(item, tokenParams)) : item));
    } else if (typeof options[option] === "object") {
      result[option] = await applyWorkspaceTokens(options[option], tokenParams, tokenizerFn);
    } else {
      result[option] = options[option];
    }
  }
  return result;
}, "applyWorkspaceTokens");

export {
  applyWorkspaceBaseTokens,
  applyWorkspaceProjectTokens,
  applyWorkspaceTokens
};
