/**
 * Trendyol Go mobil API cihaz profili (bot kayit cihazi ile hizali).
 * Service worker: importScripts ile yuklenir.
 */
(function (global) {
  'use strict';

  var TGO_MOBILE_BUILD = '1.45.4.164';
  var TGO_MOBILE_APP = 'TrendyolGo';
  var TGO_ANDROID_BUILD = 'BP2A.250605.015';

  function tgoRandomUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      var r = (Math.random() * 16) | 0;
      var v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
  }

  function normalizeTgoDeviceProfile(input) {
    if (!input || typeof input !== 'object') return null;

    var deviceId = String(input.device_id || input.deviceId || '').trim();
    var uniqueId = String(input.unique_id || input.uniqueId || '').trim();

    if (!deviceId && !uniqueId) return null;

    if (!deviceId && uniqueId) {
      deviceId = tgoRandomUuid();
    }
    if (!uniqueId && deviceId) {
      uniqueId = deviceId.replace(/-/g, '').slice(0, 16);
    }

    var sid = String(input.device_sid || input.sid || '').trim() || tgoRandomUuid();
    var pid = String(input.device_pid || input.pid || '').trim() || tgoRandomUuid();
    var tyUnique = String(input.device_ty_uniqueid || input.ty_uniqueid || input.tyUniqueId || '').trim() || uniqueId;
    var model = String(input.device_model || input.model || 'SM-A546B').trim();
    var osVersion = String(input.device_os_version || input.os_version || input.osVersion || '14').trim();

    return {
      device_id: deviceId,
      unique_id: uniqueId,
      sid: sid,
      pid: pid,
      tpay_did: String(input.tpay_did || input.tpayDid || uniqueId).trim() || uniqueId,
      ty_uniqueid: tyUnique,
      model: model,
      make: String(input.device_make || input.make || 'Samsung').trim(),
      os_version: osVersion,
    };
  }

  function buildTgoDalvikUserAgent(profile) {
    var p = profile || {};
    var model = p.model || 'SM-A546B';
    var os = p.os_version || '14';
    return 'Dalvik/2.1.0 (Linux; U; Android ' + os + '; ' + model + ' Build/' + TGO_ANDROID_BUILD + ') ' +
      TGO_MOBILE_APP + '/' + TGO_MOBILE_BUILD;
  }

  function buildTgoMobileApiHeaders(profile, bearerToken, options) {
    var p = profile || {};
    var opts = options || {};
    var channelId = opts.channelId != null ? String(opts.channelId) : '4';
    var searchSegment = opts.searchSegment != null ? String(opts.searchSegment) : '9';

    var headers = {
      'Deviceid': p.device_id || '',
      'Bff-Token-Validation': 'true',
      'X-Features': 'OPTIONAL_REBATE;TRENDYOLPAY_ENABLED;MEAL_CART_ENABLED;MEAL_CARD_TRENDYOL_PROMOTION;DELIVERY_FEE_SUMMARY_HIDDEN',
      'X-Application-Id': '5',
      'Cl_eligible': 'false',
      'Sid': p.sid || '',
      'Pid': p.pid || '',
      'Build': TGO_MOBILE_BUILD,
      'Platform': 'Android',
      'Device-Language': 'tr',
      'Osversion': p.os_version || '14',
      'Isemulator': 'false',
      'X-Channelid': channelId,
      'Uniqueid': p.unique_id || '',
      'Tpaydid': p.tpay_did || p.unique_id || '',
      'Accept-Language': 'tr-TR',
      'X-Storefront-Id': '1',
      'Searchsegment': searchSegment,
      'Gender': opts.gender || 'F',
      'X-Visitor-Type': '4',
      'Segments': '',
      'App-Name': TGO_MOBILE_APP,
      'Lc-Member': 'true',
      'Ty-Uniqueid': p.ty_uniqueid || p.unique_id || '',
      'User-Agent': buildTgoDalvikUserAgent(p),
      'Accept': 'application/json, text/plain, */*',
    };

    if (bearerToken) {
      headers.Authorization = /^bearer/i.test(bearerToken) ? bearerToken : ('bearer ' + bearerToken);
    }
    if (opts.contentType) {
      headers['Content-Type'] = opts.contentType;
    }

    return headers;
  }

  function buildTgoPaymentOptionsHeaders(profile, bearerToken) {
    var auth = bearerToken || '';
    if (auth && !/^bearer/i.test(auth)) auth = 'bearer ' + auth;

    return {
      'content-type': 'application/json',
      'app-name': TGO_MOBILE_APP,
      'x-applicationid': '1',
      'x-channelid': '4',
      'x-storefrontid': '1',
      'x-features': 'OPTIONAL_REBATE;MEAL_CART_ENABLED;MEAL_CARD_TRENDYOL_PROMOTION;PICKUP_FEE_ENABLED;VAS_QUANTITY_ENABLED;',
      'x-supported-payment-options': 'MULTINET;SODEXO;EDENRED;ON_DELIVERY;SETCARD',
      'accept-language': 'tr-TR',
      'authorization': auth,
      'user-agent': buildTgoDalvikUserAgent(profile),
      'deviceid': profile.device_id || '',
      'uniqueid': profile.unique_id || '',
      'platform': 'Android',
      'build': TGO_MOBILE_BUILD,
    };
  }

  global.TGO_MOBILE_BUILD = TGO_MOBILE_BUILD;
  global.normalizeTgoDeviceProfile = normalizeTgoDeviceProfile;
  global.buildTgoDalvikUserAgent = buildTgoDalvikUserAgent;
  global.buildTgoMobileApiHeaders = buildTgoMobileApiHeaders;
  global.buildTgoPaymentOptionsHeaders = buildTgoPaymentOptionsHeaders;
})(typeof self !== 'undefined' ? self : this);
