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,QAAS,EAC7B,CAOA,UAAO,CAAI,GACT,OAAI,aAAkB,KAAa,EAC5B,EAAE,IAAI,EAAQ,YAAc,EAAO,SAAW,IAAI,KAAK,EAChE,CAOA,MAAA,CAAO,GACL,EAAE,MAAM,KAAK,QAAS,EACxB,CAKA,SAAA,GACE,KAAK,QAAU,CAAC,CAClB,CAQA,OAAA,GACE,IAAK,MAAM,KAAQ,KAAK,QAClB,KAAK,QAAQ,eAAe,WACvB,KAAK,QAAQ,EAG1B,EAGF,OAAO,QAAU","file":"/npm/@basd/registry@0.0.9/lib/registry.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@basd/registry@0.0.9/lib/registry.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const { _, log } = require('@basd/base')\n\n/**\n * Registry class for managing different classes.\n */\nclass Registry {\n  /**\n   * Create a new Registry instance.\n   * @param {Object} config - Configuration object.\n   * @param {Object} config.classes - Initial set of classes.\n   */\n  constructor(config = {}) {\n    this.classes = {}\n    if (config.classes)\n      this.setMany(config.classes)\n  }\n\n  /**\n   * Create an instance of a class.\n   * @param {string} name - The name of the class to instantiate.\n   * @param {Array} args - The arguments to pass to the class constructor.\n   * @param {Function} baseClass - The base class for validation.\n   * @returns {Object} An instance of the class.\n   */\n  createInstance(name, args = [], baseClass) {\n    const targetClass = this.get(name)\n    if (baseClass && !this.isValidClass(targetClass, baseClass))\n      throw new Error(`Invalid ${name} class`)\n    return new targetClass(...args)\n  }\n\n  /**\n   * Check if a class is valid.\n   * @param {Function} targetClass - The class to check.\n   * @param {Function} baseClass - The base class for validation.\n   * @returns {boolean} Whether the class is valid.\n   */\n  isValidClass(targetClass, baseClass) {\n    if (!_.isFunction(targetClass)) return false\n    if (!_.isFunction(baseClass)) return true\n    return targetClass.prototype instanceof baseClass || targetClass === baseClass\n  }\n\n  /**\n   * Check if an instance is valid.\n   * @param {Object} instance - The instance to check.\n   * @param {Function} baseClass - The base class for validation.\n   * @returns {boolean} Whether the instance is valid.\n   */\n  isValidInstance(instance, baseClass) {\n    if (!_.isObj(instance)) return false\n    if (!_.isFunction(baseClass)) return true\n    return instance instanceof baseClass\n  }\n\n  /**\n   * Ingest classes from a source.\n   * @param {Object} source - The source object.\n   */\n  ingest(source) {\n    const classes = _.merge({}, _.get(source, 'constructor.classes'), _.get(source, 'opts.classes'))\n    this.setMany(classes)\n  }\n\n  /**\n   * Register a class with additional validation.\n   * @param {string} name - The name of the class.\n   * @param {Function} targetClass - The class to register.\n   * @param {Function} baseClass - The base class for validation.\n   * @param {boolean} override - Whether to override existing class.\n   */\n  setWith(name, targetClass, baseClass, override = false) {\n    if (!this.isValidClass(targetClass, baseClass))\n      throw new Error(`Invalid class`)\n    this.set(name, targetClass, override)\n  }\n\n  /**\n   * Register a class.\n   * @param {string} name - The name of the class.\n   * @param {Function} targetClass - The class to register.\n   * @param {boolean} override - Whether to override existing class.\n   */\n  set(name, targetClass, override = true) {\n    if (override || !this.has(name))\n      _.setWith(this.classes, name, targetClass, Object)\n  }\n\n  /**\n   * Register multiple classes.\n   * @param {Object} classes - An object mapping names to classes.\n   * @param {boolean} override - Whether to override existing classes.\n   */\n  setMany(classes, override = false) {\n    for (const [key, value] of _.entries(classes))\n      this.set(key, value, override)\n  }\n\n  /**\n   * Retrieve a class by name.\n   * @param {string} name - The name of the class.\n   * @param {*} defaultValue - The default value if the class does not exist.\n   * @returns {Function|null} The class or null.\n   */\n  get(name, defaultValue = null) {\n    return _.get(this.classes, name, defaultValue)\n  }\n\n  /**\n   * Check if a class exists by name.\n   * @param {string} name - The name of the class.\n   * @returns {boolean} Whether the class exists.\n   */\n  has(name) {\n    return _.has(this.classes, name)\n  }\n\n  /**\n   * Static method to get a Registry instance.\n   * @param {Object} config - Configuration object or existing Registry instance.\n   * @returns {Registry} A Registry instance.\n   */\n  static get(config) {\n    if (config instanceof this) return config\n    return _.get(config, 'registry') ? config.registry : new this(config)\n  }\n\n  /**\n   * Removes a specific class from the registry.\n   * \n   * @param {string} name - The name of the class to be removed from the registry.\n   */\n  remove(name) {\n    _.unset(this.classes, name)\n  }\n\n  /**\n   * Removes all classes from the registry.\n   */\n  removeAll() {\n    this.classes = {}\n  }\n\n  /**\n   * Destroys the registry by deleting all properties of the classes object.\n   * This method ensures that all class references in the registry are \n   * cleared, potentially allowing them to be garbage collected if there\n   * are no other references to them.\n   */\n  destroy() {\n    for (const prop in this.classes) {\n      if (this.classes.hasOwnProperty(prop)) {\n        delete this.classes[prop]\n      }\n    }\n  }\n}\n\nmodule.exports = Registry\n"]}