<commit_before>fs = require "fs-extra"
path = require "path"
plist = require "plist"
Promise = require "promise"
semver = require "semver"
request = require "./request"
cache = require "./cache"
config = require "./config"
util = require "./util"

readFile = Promise.denodeify(fs.readFile)

###*
 * データ
 ###
data = {}

###*
 * エラー出力
 * @private
 ###
_outputError = (err) ->
  console.error("Error: #{err}") if err?
  return

###*
 * 使いやすいように変形します
 * @param {Object} plist.parse()で変換したもの
 * @return {Object}
 ###
parse = (plistObj) ->
  obj = {}
  for key, val of plistObj
    [name, id] = key.split(":")
    obj[name] = {}
    for k, v of val
      [n, i] = k.split(":")
      obj[name][n] = {}
      for k_, v_ of v
        [n_, i_] = k_.split(":")
        if v_? and v_ isnt ""
          [version, platform] = v_.split(":")
        else
          [version, platform] = ["", ""]
        obj[name][n][n_] =
          name: "#{id}.#{i}.#{i_}"
          version: version
          platform: platform
  return obj

###*
 * plistを取得できるまで取得(言語順)
 * @param {repo: "remote"|"local", name: string} repo
 * @param {string} lang
 * @param {boolean} force キャッシュを無視して元ファイルを取得するか 既定値は"false"
 * @return {Object} plistのオブジェクト
 ###
getUntilDone = (repo, lang, force = false) ->
  return get(repo, lang, force).catch( ->
    return get(repo, "en", force)
  ).catch( ->
    return get(repo, "ja", force)
  ).catch( ->
    return get(repo, "ru", force)
  )

###*
 * plistを取得してパースしたものを返します
 * @param {"remote"|"local"} repoType
 * @param {string} repoName
 * @param {string} lang<commit_msg>Update: zh_CH,zh_TWのときzh.plistを読み込むように<commit_after>fs = require "fs-extra"
path = require "path"
plist = require "plist"
Promise = require "promise"
semver = require "semver"
request = require "./request"
cache = require "./cache"
config = require "./config"
util = require "./util"

readFile = Promise.denodeify(fs.readFile)

###*
 * データ
 ###
data = {}

###*
 * エラー出力
 * @private
 ###
_outputError = (err) ->
  console.error("Error: #{err}") if err?
  return

###*
 * 使いやすいように変形します
 * @param {Object} plist.parse()で変換したもの
 * @return {Object}
 ###
parse = (plistObj) ->
  obj = {}
  for key, val of plistObj
    [name, id] = key.split(":")
    obj[name] = {}
    for k, v of val
      [n, i] = k.split(":")
      obj[name][n] = {}
      for k_, v_ of v
        [n_, i_] = k_.split(":")
        if v_? and v_ isnt ""
          [version, platform] = v_.split(":")
        else
          [version, platform] = ["", ""]
        obj[name][n][n_] =
          name: "#{id}.#{i}.#{i_}"
          version: version
          platform: platform
  return obj

###*
 * plistを取得できるまで取得(言語順)
 * @param {repo: "remote"|"local", name: string} repo
 * @param {string} lang
 * @param {boolean} force キャッシュを無視して元ファイルを取得するか 既定値は"false"
 * @return {Object} plistのオブジェクト
 ###
getUntilDone = (repo, lang, force = false) ->
  return get(repo, lang, force).catch(->
    if lang.includes("_")
      return get(repo, lang.split("_")[0], force)
    return Promise.reject()
  ).catch( ->
    return get(repo, "en", force)
  ).catch( ->
    return get(repo, "ja", force)
  ).catch( ->
    return get(repo, "ru", force)
  )

###*
 * plistを取得してパースしたものを返します
 * @param {"remote"|"local"} repoType
 * @param {string} repoName
 * @param {string} lang
{'fraction_non_alphanumeric': 0.19202678027997566, 'fraction_numerical': 0.00030432136335970786, 'mean_word_length': 2.927120669056153, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1847492', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1309, 'n_words': 301}