{"version":3,"file":"question.mjs","sources":["../../../../src/digitalmarketplace/components/question/question.ts"],"sourcesContent":["import { DigitalMarketplaceFrontendComponent } from '../../digitalmarketplace-frontend-component'\n\nenum QuestionType {\n  CONTROLLER,\n  CONTROLLER_AND_TARGET,\n  TARGET\n}\n\nclass QuestionInput {\n  question: QuestionQuestion\n  $input: JQuery<HTMLInputElement>\n  targets: string[]\n\n  constructor (question: QuestionQuestion, $input: JQuery<HTMLInputElement>) {\n    this.question = question\n    this.$input = $input\n\n    const targets = this.$input.attr('data-target')\n    this.targets = targets ? targets.split(' ') : []\n  }\n\n  setEventListener () {\n    this.$input.on('click', () => {\n      this.question.updateTargetQuestionsVisibility()\n    })\n  }\n\n  isChecked () {\n    return this.$input.is(':checked')\n  }\n}\n\nclass QuestionQuestion {\n  questionWatcher: Question\n  $question: JQuery<HTMLElement>\n  id: string\n  questionType: QuestionType\n  questionInputs?: QuestionInput[]\n  possibleTargetIds?: Set<string>\n  targetOf?: string[]\n\n  constructor (questionWatcher: Question, $question: JQuery<HTMLElement>, questionType: QuestionType) {\n    this.questionWatcher = questionWatcher\n    this.$question = $question\n    this.id = this.$question.attr('id') as string\n    this.questionType = questionType\n\n    if (this.questionType === QuestionType.CONTROLLER) {\n      this.enableControllerQuestionMethods()\n    } else {\n      this.enableTargetQuestionMethods()\n    }\n  }\n\n  enableControllerQuestionMethods () {\n    this.questionInputs = this.$question.find('input').get().map((inputElement) => new QuestionInput(this, $(inputElement)))\n\n    this.possibleTargetIds = new Set(this.questionInputs.map((questionInput) => questionInput.targets).flat())\n  }\n\n  enableTargetQuestionMethods () {\n    this.targetOf = []\n  }\n\n  isVisible () {\n    return !this.$question.hasClass('govuk-visually-hidden')\n  }\n\n  currentTargetIds () {\n    let targets: string[]\n\n    if (this.isVisible() && this.questionInputs) {\n      targets = this.questionInputs.filter((questionInput) => questionInput.isChecked()).map((questionInput) => questionInput.targets).flat()\n    } else {\n      targets = []\n    }\n\n    return new Set(targets)\n  }\n\n  updateTargetQuestionsVisibility () {\n    this.questionWatcher.updateTargetQuestionsVisibility(this.possibleTargetIds)\n  }\n\n  appendTargetOf (questionId: string) {\n    if (this.targetOf !== undefined) {\n      this.targetOf.push(questionId)\n    }\n  }\n\n  toggleVisibility (isShown: boolean) {\n    this.$question.toggleClass('govuk-visually-hidden', !isShown)\n    this.$question.attr('aria-hidden', (!isShown).toString())\n    if (isShown) {\n      this.$question.removeAttr('hidden')\n    } else {\n      this.$question.attr('hidden', '')\n    }\n  }\n}\n\nclass Question implements DigitalMarketplaceFrontendComponent {\n  static moduleName = 'dm-question'\n  questions: Record<string, QuestionQuestion>\n\n  constructor () {\n    this.questions = {}\n\n    $(`[data-module=\"${Question.moduleName}\"]`).each((_index, question) => {\n      this.push(new QuestionQuestion(this, $(question), QuestionType.CONTROLLER))\n    })\n\n    for (const [questionId, question] of Object.entries(this.questions)) {\n      for (const questionTargetId of question.possibleTargetIds || []) {\n        if (questionTargetId in this.questions) {\n          if (this.questions[questionTargetId].questionType === QuestionType.CONTROLLER) {\n            this.questions[questionTargetId].questionType = QuestionType.CONTROLLER_AND_TARGET\n            this.questions[questionTargetId].enableTargetQuestionMethods()\n          }\n        } else {\n          this.push(new QuestionQuestion(this, $(`#${questionTargetId}`), QuestionType.TARGET))\n        }\n\n        this.questions[questionTargetId].appendTargetOf(questionId)\n      }\n    }\n  }\n\n  init () {\n    for (const question of Object.values(this.questions)) {\n      if (question.questionType < QuestionType.TARGET) {\n        for (const questionInput of question.questionInputs || []) {\n          questionInput.setEventListener()\n        }\n        if (question.questionType === QuestionType.CONTROLLER) {\n          this.updateTargetQuestionsVisibility(question.possibleTargetIds)\n        }\n      }\n    }\n  }\n\n  push (question: QuestionQuestion) {\n    this.questions[question.id] = question\n  }\n\n  updateTargetQuestionsVisibility (possibleTargetIds?: Set<string>, isShown?: boolean) {\n    for (const possibleTargetId of possibleTargetIds || []) {\n      const targetQuestion = this.questions[possibleTargetId]\n      let questionIsShown\n\n      if (isShown === undefined) {\n        const controllerQuestions = targetQuestion.targetOf?.map((questionId) => this.questions[questionId])\n\n        const targetsToShow = new Set(controllerQuestions?.map((controllerQuestion) => Array.from(controllerQuestion.currentTargetIds())).flat())\n\n        questionIsShown = targetsToShow.has(possibleTargetId)\n      } else {\n        questionIsShown = isShown\n      }\n\n      targetQuestion.toggleVisibility(questionIsShown)\n\n      if (targetQuestion.questionType === QuestionType.CONTROLLER_AND_TARGET) {\n        if (questionIsShown) {\n          this.updateTargetQuestionsVisibility(targetQuestion.possibleTargetIds)\n        } else {\n          this.updateTargetQuestionsVisibility(targetQuestion.possibleTargetIds, false)\n        }\n      }\n    }\n  }\n}\n\nexport { Question }\n"],"names":[],"mappings":"AAEA,IAAK,YAIJ;AAJD,CAAA,UAAK,YAAY,EAAA;AACf,IAAA,YAAA,CAAA,YAAA,CAAA,YAAA,CAAA,GAAA,CAAA,CAAA,GAAA,YAAU;AACV,IAAA,YAAA,CAAA,YAAA,CAAA,uBAAA,CAAA,GAAA,CAAA,CAAA,GAAA,uBAAqB;AACrB,IAAA,YAAA,CAAA,YAAA,CAAA,QAAA,CAAA,GAAA,CAAA,CAAA,GAAA,QAAM;AACR,CAAC,EAJI,YAAY,KAAZ,YAAY,GAAA,EAAA,CAAA,CAAA;AAMjB,MAAM,aAAa,CAAA;AACjB,IAAA,QAAQ;AACR,IAAA,MAAM;AACN,IAAA,OAAO;IAEP,WAAA,CAAa,QAA0B,EAAE,MAAgC,EAAA;AACvE,QAAA,IAAI,CAAC,QAAQ,GAAG,QAAQ;AACxB,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM;QAEpB,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC;AAC/C,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;IAClD;IAEA,gBAAgB,GAAA;QACd,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAA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