const { createDownloadUrl } = require("../../proxy-url");
const { getGitRevision } = require("../../linker");
const MobileDetect = require('mobile-detect');

exports.createModel = function(req) {
  let buildTime = require("../../build-time").string;
  let isMobile = !!(new MobileDetect(req.headers['user-agent'])).mobile();
  let downloadUrl = null;
  let clip = req.shot.getClip(req.shot.clipNames()[0]);
  if (clip) {
    downloadUrl = createDownloadUrl(clip.image.url, req.shot.filename);
  }
  let serverPayload = {
    title: req.shot.title,
    staticLink: req.staticLink,
    backend: req.backend,
    shot: req.shot,
    contentOrigin: req.config.contentOrigin,
    contentProtocol: req.protocol,
    id: req.shot.id,
    productName: req.config.productName,
    isExtInstalled: !!req.deviceId,
    isOwner: req.deviceId == req.shot.ownerId,
    gaId: req.config.gaId,
    deviceId: req.deviceId,
    authenticated: !!req.deviceId,
    buildTime,
    simple: false,
    shotDomain: req.url, // FIXME: should be a property of the shot
    expireTime: req.shot.expireTime === null ? null : req.shot.expireTime.getTime(),
    retentionTime: req.config.expiredRetentionTime * 1000,
    defaultExpiration: req.config.defaultExpiration * 1000,
    sentryPublicDSN: req.config.sentryPublicDSN,
    cspNonce: req.cspNonce,
    hashAnalytics: true,
    userAgent: req.headers['user-agent'],
    blockType: req.shot.blockType,
    downloadUrl,
    isMobile
  };
  let clientPayload = {
    title: req.shot.title,
    gitRevision: getGitRevision(),
    backend: req.backend,
    shot: req.shot.asJson(),
    contentOrigin: req.config.contentOrigin,
    contentProtocol: req.protocol,
    id: req.shot.id,
    productName: req.config.productName,
    isExtInstalled: !!req.deviceId,
    isOwner: req.deviceId == req.shot.ownerId,
    gaId: req.config.gaId,
    deviceId: req.deviceId,
    authenticated: !!req.deviceId,
    shotDomain: req.url,
    urlIfDeleted: req.shot.urlIfDeleted,
    expireTime: req.shot.expireTime === null ? null : req.shot.expireTime.getTime(),
    deleted: req.shot.deleted,
    buildTime,
    simple: false,
    retentionTime: req.config.expiredRetentionTime * 1000,
    defaultExpiration: req.config.defaultExpiration * 1000,
    hashAnalytics: true,
    userAgent: req.headers['user-agent'],
    blockType: req.shot.blockType,
    downloadUrl,
    isMobile
  };
  if (serverPayload.expireTime !== null && Date.now() > serverPayload.expireTime) {
    clientPayload.shot = {
      url: req.shot.url,
      docTitle: req.shot.title
    };
    serverPayload.shot = Object.assign({
      urlIfDeleted: req.shot.urlIfDeleted,
      title: req.shot.title
    }, clientPayload.shot);
  }
  return Promise.resolve({serverModel: serverPayload, jsonModel: clientPayload});
};

@@ -1,15 +1,31 @@
 const %7B
+ createProxyUrl,
  createD
@@ -506,16 +506,108 @@
 e);%0A  %7D%0A
+  if (req.shot.favicon) %7B%0A    req.shot.favicon = createProxyUrl(req, req.shot.favicon);%0A  %7D%0A
   let se

use proxy url for favicon on shot page