{"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/computed-properties/core/src/index.ts"],"sourcesContent":["import type {\n  Player,\n  PlayerPlugin,\n  ExpressionEvaluator,\n  Schema,\n  Expression,\n  DataModelMiddleware,\n  Updates,\n  SchemaController,\n  BindingInstance,\n} from \"@player-ui/player\";\n\nexport interface ExpressionDataType extends Schema.DataType<\"Expression\"> {\n  /** The expression to evaluate to compute the value of this node */\n  exp?: Expression;\n}\n\n/**\n * A player plugin to manage beacon events.\n * It automatically keeps track of the current user's view, and adds additional metaData to each beacon event.\n */\nexport class ComputedPropertiesPlugin implements PlayerPlugin {\n  name = \"ComputedProperties\";\n\n  apply(player: Player) {\n    // Inject a middleware into the data-model to intercept any calls to an expression type in the schema\n    // Calls to `set` throw an error. Calls to `get` compute the value and return if or default\n\n    let schemaController: SchemaController | undefined;\n    let expressionEvaluator: ExpressionEvaluator | undefined;\n\n    /** Look up the data-type of the binding, and check for an Expression type */\n    const getExpressionType = (\n      binding: BindingInstance,\n    ): ExpressionDataType | undefined => {\n      // Check to see if the data-type of the binding is an `Expression`\n      const dataType = schemaController?.getType(binding);\n\n      if (dataType?.type === \"Expression\") {\n        return dataType as ExpressionDataType;\n      }\n    };\n\n    const computedPropertyMiddleware: DataModelMiddleware = {\n      name: this.name,\n      get(binding, options, next) {\n        const expType = getExpressionType(binding);\n\n        if (expType) {\n          const { exp } = expType;\n          const result =\n            exp && expressionEvaluator?.evaluate(exp, options?.context);\n\n          return result ?? expType.default;\n        }\n\n        return next?.get(binding, options);\n      },\n      set(transaction, options, next): Updates {\n        for (const setOperation of transaction) {\n          if (getExpressionType(setOperation[0])) {\n            throw new Error(\n              `Invalid 'set' operation on computed property: ${setOperation[0].asString()}`,\n            );\n          }\n        }\n\n        return next?.set(transaction, options) ?? [];\n      },\n      delete(binding, options, next) {\n        if (getExpressionType(binding)) {\n          throw new Error(\n            `Invalid 'delete' operation on computed property: ${binding.asString()}`,\n          );\n        }\n\n        return next?.delete(binding, options);\n      },\n    };\n\n    player.hooks.dataController.tap(this.name, (dataController) => {\n      dataController.hooks.resolveDataStages.tap(this.name, (dataPipeline) => {\n        return [...dataPipeline, computedPropertyMiddleware];\n      });\n    });\n\n    player.hooks.schema.tap(this.name, (schema) => {\n      schemaController = schema;\n    });\n\n    player.hooks.expressionEvaluator.tap(this.name, (evaluator) => {\n      expressionEvaluator = evaluator;\n    });\n  }\n}\n"],"mappings":";AAqBO,IAAM,2BAAN,MAAuD;AAAA,EAAvD;AACL,gBAAO;AAAA;AAAA,EAEP,MAAM,QAAgB;AAIpB,QAAI;AACJ,QAAI;AAGJ,UAAM,oBAAoB,CACxB,YACmC;AAEnC,YAAM,WAAW,kBAAkB,QAAQ,OAAO;AAElD,UAAI,UAAU,SAAS,cAAc;AACnC,eAAO;AAAA,MACT;AAAA,IACF;AAEA,UAAM,6BAAkD;AAAA,MACtD,MAAM,KAAK;AAAA,MACX,IAAI,SAAS,SAAS,MAAM;AAC1B,cAAM,UAAU,kBAAkB,OAAO;AAEzC,YAAI,SAAS;AACX,gBAAM,EAAE,IAAI,IAAI;AAChB,gBAAM,SACJ,OAAO,qBAAqB,SAAS,KAAK,SAAS,OAAO;AAE5D,iBAAO,UAAU,QAAQ;AAAA,QAC3B;AAEA,eAAO,MAAM,IAAI,SAAS,OAAO;AAAA,MACnC;AAAA,MACA,IAAI,aAAa,SAAS,MAAe;AACvC,mBAAW,gBAAgB,aAAa;AACtC,cAAI,kBAAkB,aAAa,CAAC,CAAC,GAAG;AACtC,kBAAM,IAAI;AAAA,cACR,iDAAiD,aAAa,CAAC,EAAE,SAAS,CAAC;AAAA,YAC7E;AAAA,UACF;AAAA,QACF;AAEA,eAAO,MAAM,IAAI,aAAa,OAAO,KAAK,CAAC;AAAA,MAC7C;AAAA,MACA,OAAO,SAAS,SAAS,MAAM;AAC7B,YAAI,kBAAkB,OAAO,GAAG;AAC9B,gBAAM,IAAI;AAAA,YACR,oDAAoD,QAAQ,SA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