var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
var __runInitializers = (array, flags, self, value) => {
  for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
  return value;
};
var __decorateElement = (array, flags, name, decorators, target, extra) => {
  var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
  var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
  var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
  var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
    return __privateGet(this, extra);
  }, set [name](x) {
    return __privateSet(this, extra, x);
  } }, name));
  k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
  for (var i = decorators.length - 1; i >= 0; i--) {
    ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
    if (k) {
      ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
      if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
      if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
    }
    it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
    if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
    else if (typeof it !== "object" || it === null) __typeError("Object expected");
    else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
  }
  return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
};
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);

// src/core/vcs/hooks.ts
import { AsyncSeriesWaterfallHook } from "tapable";
import { generateHooksAccess } from "@diplodoc/cli/lib/utils";
function hooks(name) {
  return {
    VcsConnector: new AsyncSeriesWaterfallHook(
      ["connector"],
      `${name}.VcsConnector`
    )
  };
}
var [getHooks, withHooks] = generateHooksAccess("Vcs", hooks);

// src/core/vcs/VcsService.ts
import { join as join2 } from "node:path";
import { all, bounded, memoize, normalizePath } from "@diplodoc/cli/lib/utils";

// src/core/vcs/connector.ts
import { join } from "node:path";
var DefaultVcsConnector = class {
  constructor(run) {
    this.run = run;
  }
  getData() {
    return {
      mtimes: {},
      authors: {},
      contributors: {}
    };
  }
  setData() {
  }
  async getBase() {
    return ".";
  }
  async getContributorsByPath() {
    return [];
  }
  async getModifiedTimeByPath(path) {
    const stat = await this.run.fs.stat(join(this.run.originalInput, path));
    return Math.round(stat.mtimeMs / 1e3);
  }
  async getAuthorByPath() {
    return null;
  }
  async getUserByLogin() {
    return null;
  }
};

// src/core/vcs/VcsService.ts
var _realpath_dec, _getMTime_dec, _getContributors_dec, _getBase_dec, _VcsService_decorators, _init;
_VcsService_decorators = [withHooks], _getBase_dec = [memoize()], _getContributors_dec = [memoize("path")], _getMTime_dec = [memoize("path")], _realpath_dec = [bounded];
var VcsService = class {
  constructor(run) {
    __runInitializers(_init, 5, this);
    this.name = "Vcs";
    this.run = void 0;
    this.config = void 0;
    this.connector = void 0;
    this.run = run;
    this.connector = new DefaultVcsConnector(run);
    this.config = run.config;
  }
  async init() {
    if (!this.config.vcs.enabled) {
      return;
    }
    this.connector = await getHooks(this).VcsConnector.promise(this.connector);
  }
  getData() {
    return this.connector.getData();
  }
  setData(data) {
    this.connector.setData(data);
  }
  async metadata(path, deps = []) {
    const file = normalizePath(path);
    const meta = this.run.meta.get(file);
    const result = {};
    const [vcsPath, author, contributors, updatedAt] = await Promise.all([
      this.config.vcsPath.enabled ? this.realpath(file) : void 0,
      this.config.authors.enabled ? this.getAuthor(file, meta?.author) : void 0,
      this.config.contributors.enabled ? this.getContributors(file, deps) : [],
      this.config.mtimes.enabled ? this.getMTime(file, deps) : void 0
    ]);
    result.vcsPath = vcsPath || void 0;
    if (!(this.connector instanceof DefaultVcsConnector)) {
      result.author = author || void 0;
    }
    result.contributors = contributors.length ? contributors : void 0;
    result.updatedAt = updatedAt || void 0;
    Object.assign(result, await this.getResources(file, result));
    return result;
  }
  async getBase() {
    return this.connector.getBase();
  }
  async getContributorsByPath(path, deps = []) {
    return this.connector.getContributorsByPath(
      await this.realpath(path),
      await all(deps.map(this.realpath))
    );
  }
  async getModifiedTimeByPath(path) {
    return this.connector.getModifiedTimeByPath(await this.realpath(path));
  }
  async getAuthorByPath(path) {
    return this.connector.getAuthorByPath(await this.realpath(path));
  }
  async getUserByLogin(author) {
    return this.connector.getUserByLogin(author);
  }
  async getResources(path, meta) {
    return this.connector.getResourcesByPath?.(path, meta) || {};
  }
  async getAuthor(path, author) {
    if (!author) {
      return this.getAuthorByPath(path);
    }
    if (typeof author === "object") {
      return author;
    }
    try {
      return JSON.parse(author);
    } catch {
      return this.getUserByLogin(author);
    }
  }
  async getContributors(path, deps) {
    const contributors = await this.getContributorsByPath(path, deps);
    return Object.values(contributors);
  }
  async getMTime(path, deps) {
    const mtimes = [];
    const files = [path].concat(deps);
    for (const file of files) {
      const mtime = await this.getModifiedTimeByPath(file);
      if (typeof mtime === "number") {
        mtimes.push(mtime);
      }
    }
    if (!mtimes.length) {
      return void 0;
    }
    return new Date(Math.max(...mtimes) * 1e3).toISOString();
  }
  async realpath(file) {
    const base = await this.getBase();
    const meta = this.run.meta.get(file);
    if (meta.vcsPath) {
      return normalizePath(meta.vcsPath);
    }
    if (meta.sourcePath) {
      return normalizePath(meta.sourcePath);
    }
    return normalizePath(join2(base, file));
  }
};
_init = __decoratorStart(null);
__decorateElement(_init, 1, "getBase", _getBase_dec, VcsService);
__decorateElement(_init, 1, "getContributors", _getContributors_dec, VcsService);
__decorateElement(_init, 1, "getMTime", _getMTime_dec, VcsService);
__decorateElement(_init, 1, "realpath", _realpath_dec, VcsService);
VcsService = __decorateElement(_init, 0, "VcsService", _VcsService_decorators, VcsService);
__runInitializers(_init, 1, VcsService);
export {
  VcsService,
  getHooks
};
//# sourceMappingURL=index.mjs.map
