"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StagedConfig = void 0;
function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));
  _fsExtra = function () {
    return data;
  };
  return data;
}
function _path() {
  const data = _interopRequireDefault(require("path"));
  _path = function () {
    return data;
  };
  return data;
}
function _componentId() {
  const data = require("@teambit/component-id");
  _componentId = function () {
    return data;
  };
  return data;
}
function _laneId() {
  const data = require("@teambit/lane-id");
  _laneId = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const STAGED_CONFIG_DIR = 'staged-config';
class StagedConfig {
  constructor(filePath, componentsConfig, logger) {
    this.filePath = filePath;
    this.componentsConfig = componentsConfig;
    this.logger = logger;
    _defineProperty(this, "hasChanged", false);
  }
  static async load(scopePath, logger, laneId) {
    const lanePath = laneId ? _path().default.join(laneId.scope, laneId.name) : _laneId().DEFAULT_LANE;
    const filePath = _path().default.join(scopePath, STAGED_CONFIG_DIR, `${lanePath}.json`);
    let componentsConfig = [];
    try {
      const fileContent = await _fsExtra().default.readJson(filePath);
      componentsConfig = fileContent.map(item => ({
        id: _componentId().ComponentID.fromObject(item.id),
        config: item.config
      }));
    } catch (err) {
      if (err.code === 'ENOENT') {
        componentsConfig = [];
      } else {
        throw err;
      }
    }
    return new StagedConfig(filePath, componentsConfig, logger);
  }
  toObject() {
    return this.componentsConfig.map(({
      id,
      config
    }) => ({
      id: id.toObject(),
      config
    }));
  }
  async write() {
    if (!this.hasChanged) return;
    await _fsExtra().default.outputFile(this.filePath, JSON.stringify(this.toObject(), null, 2));
  }
  getConfigPerId(id) {
    return this.componentsConfig.find(c => c.id.isEqual(id, {
      ignoreVersion: true
    }))?.config;
  }
  getAll() {
    return this.componentsConfig;
  }
  isEmpty() {
    return this.componentsConfig.length === 0;
  }
  async deleteFile() {
    this.logger.debug(`staged-config, deleting ${this.filePath}`);
    await _fsExtra().default.remove(this.filePath);
    this.componentsConfig = [];
  }
  addComponentConfig(id, config) {
    const exists = this.componentsConfig.find(c => c.id.isEqual(id, {
      ignoreVersion: true
    }));
    if (exists) {
      exists.config = config;
    } else {
      this.componentsConfig.push({
        id,
        config
      });
    }
    this.hasChanged = true;
  }
  removeComponentConfig(id) {
    const componentsConfigLengthBefore = this.componentsConfig.length;
    this.componentsConfig = this.componentsConfig.filter(c => !c.id.isEqual(id, {
      ignoreVersion: true
    }));
    if (this.componentsConfig.length === componentsConfigLengthBefore) {
      this.logger.debug(`removeComponentConfig: unable to find ${id.toString()}`);
    } else {
      this.hasChanged = true;
    }
  }
}
exports.StagedConfig = StagedConfig;

//# sourceMappingURL=staged-config.js.map