"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;
}
const _excluded = ["id"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return 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 => _objectSpread(_objectSpread({}, item), {}, {
        id: _componentId().ComponentID.fromObject(item.id)
      }));
    } catch (err) {
      if (err.code === 'ENOENT') {
        componentsConfig = [];
      } else {
        throw err;
      }
    }
    return new StagedConfig(filePath, componentsConfig, logger);
  }
  toObject() {
    return this.componentsConfig.map(_ref => {
      let {
          id
        } = _ref,
        rest = _objectWithoutProperties(_ref, _excluded);
      return _objectSpread({
        id: id.toObject()
      }, rest);
    });
  }
  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;
  }
  getPerId(id) {
    return this.componentsConfig.find(c => c.id.isEqual(id, {
      ignoreVersion: true
    }));
  }
  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, componentMapObject) {
    const exists = this.componentsConfig.find(c => c.id.isEqual(id, {
      ignoreVersion: true
    }));
    if (exists) {
      exists.config = config;
    } else {
      this.componentsConfig.push({
        id,
        config,
        componentMapObject
      });
    }
    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