{"version":3,"file":"component.mjs","sources":["../../src/govuk/component.mjs"],"sourcesContent":["import { isInitialised, isSupported } from './common/index.mjs'\nimport { ElementError, InitError, SupportError } from './errors/index.mjs'\n\n/**\n * Base Component class\n *\n * Centralises the behaviours shared by our components\n *\n * @virtual\n * @template {Element} [RootElementType=HTMLElement]\n */\nexport class Component {\n  /**\n   * @type {typeof Element}\n   */\n  static elementType = HTMLElement\n\n  // allows Typescript user to work around the lack of types\n  // in GOVUKFrontend package, Typescript is not aware of $root\n  // in components that extend GOVUKFrontendComponent\n  /**\n   * Returns the root element of the component\n   *\n   * @protected\n   * @returns {RootElementType} - the root element of component\n   */\n  get $root() {\n    return this._$root\n  }\n\n  /**\n   * @protected\n   * @type {RootElementType}\n   */\n  _$root\n\n  /**\n   * Constructs a new component, validating that GOV.UK Frontend is supported\n   *\n   * @internal\n   * @param {Element | null} [$root] - HTML element to use for component\n   */\n  constructor($root) {\n    const childConstructor = /** @type {ChildClassConstructor} */ (\n      this.constructor\n    )\n\n    // TypeScript does not enforce that inheriting classes will define a `moduleName`\n    // (even if we add a `@virtual` `static moduleName` property to this class).\n    // While we trust users to do this correctly, we do a little check to provide them\n    // a helpful error message.\n    //\n    // After this, we'll be sure that `childConstructor` has a `moduleName`\n    // as expected of the `ChildClassConstructor` we've cast `this.constructor` to.\n    if (typeof childConstructor.moduleName !== 'string') {\n      throw new InitError(`\\`moduleName\\` not defined in component`)\n    }\n\n    if (!($root instanceof childConstructor.elementType)) {\n      throw new ElementError({\n        element: $root,\n        component: childConstructor,\n        identifier: 'Root element (`$root`)',\n        expectedType: childConstructor.elementType.name\n      })\n    } else {\n      this._$root = /** @type {RootElementType} */ ($root)\n    }\n\n    childConstructor.checkSupport()\n\n    this.checkInitialised()\n\n    const moduleName = childConstructor.moduleName\n\n    this.$root.setAttribute(`data-${moduleName}-init`, '')\n  }\n\n  /**\n   * Validates whether component is already initialised\n   *\n   * @private\n   * @throws {InitError} when component is already initialised\n   */\n  checkInitialised() {\n    const constructor = /** @type {ChildClassConstructor} */ (this.constructor)\n    const moduleName = constructor.moduleName\n\n    if (moduleName && isInitialised(this.$root, moduleName)) {\n      throw new InitError(constructor)\n    }\n  }\n\n  /**\n   * Validates whether components are supported\n   *\n   * @throws {SupportError} when the components are not supported\n   */\n  static checkSupport() {\n    if (!isSupported()) {\n      throw new SupportError()\n    }\n  }\n}\n\n/**\n * @typedef ChildClass\n * @property {string} moduleName - The module name that'll be looked for in the DOM when initialising the component\n */\n\n/**\n * @typedef {typeof Component & ChildClass} ChildClassConstructor\n */\n"],"names":["Component","$root","_$root","constructor","childConstructor","moduleName","InitError","elementType","ElementError","element","component","identifier","expectedType","name","checkSupport","checkInitialised","setAttribute","isInitialised","isSupported","SupportError","HTMLElement"],"mappings":";;;AAWO,MAAMA,SAAS,CAAC;AASrB;AACF;AACA;AACA;AACA;AACA;EACE,IAAIC,KAAKA,GAAG;IACV,OAAO,IAAI,CAACC,MAAM;AACpB,EAAA;EAcAC,WAAWA,CAACF,KAAK,EAAE;AAAA,IAAA,IAAA,CARnBC,MAAM,GAAA,MAAA;AASJ,IAAA,MAAME,gBAAgB,GACpB,IAAI,CAACD,WACN;AASD,IAAA,IAAI,OAAOC,gBAAgB,CAACC,UAAU,KAAK,QAAQ,EAAE;AACnD,MAAA,MAAM,IAAIC,SAAS,CAAC,CAAA,uCAAA,CAAyC,CAAC;AAChE,IAAA;AAEA,IAAA,IAAI,EAAEL,KAAK,YAAYG,gBAAgB,CAACG,WAAW,CAAC,EAAE;MACpD,MAAM,IAAIC,YAAY,CAAC;AACrBC,QAAAA,OAAO,EAAER,KAAK;AACdS,QAAAA,SAAS,EAAEN,gBAAgB;AAC3BO,QAAAA,UAAU,EAAE,wBAAwB;AACpCC,QAAAA,YAAY,EAAER,gBAAgB,CAACG,WAAW,CAACM;AAC7C,OAAC,CAAC;AACJ,IAAA,CAAC,MAAM;MACL,IAAI,CAACX,MAAM,GAAmCD,KAAM;AACtD,IAAA;IAEAG,gBAAgB,CA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