local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local loadfile = _tl_compat and _tl_compat.loadfile or loadfile; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type

local file_discovery = require("cerulean.file_discovery")

local FormatterOptions = {}









local options = { FormatterOptions = FormatterOptions }

function FormatterOptions.new(
   check_only,
   log_level,
   indent_width,
   max_line_width,
   sort_requires,
   files,
   read_stdin)

   local self = setmetatable({}, { __index = FormatterOptions })
   self.check_only = check_only
   self.log_level = log_level
   self.indent_width = indent_width
   self.max_line_width = max_line_width
   self.sort_requires = sort_requires
   self.files = files
   self.read_stdin = read_stdin
   return self
end

function options.default()
   return FormatterOptions.new(false, "nothing", 4, 88, true, {}, false)
end

local function parse_positive_integer(raw, flag)
   local n = math.tointeger(tonumber(raw))
   if not n or n <= 0 then
      return nil, string.format("%s requires a positive integer, got '%s'", flag, raw)
   end
   return n, nil
end

local function apply_args(
   apply_to, script_args)

   local i = 1
   while i <= #script_args do
      local value = script_args[i]
      if value == "--check" then
         apply_to.check_only = true
      elseif value == "--no-sort-requires" then
         apply_to.sort_requires = false
      elseif value == "--log-level" then
         i = i + 1
         if i > #script_args then
            return nil, "--log-level requires an argument"
         end
         apply_to.log_level = script_args[i]
      elseif value == "--indent" then
         i = i + 1
         if i > #script_args then
            return nil, "--indent requires an argument"
         end
         local n, err = parse_positive_integer(script_args[i], "--indent")
         if err then
            return nil, err
         end
         apply_to.indent_width = n
      elseif value == "--line-length" then
         i = i + 1
         if i > #script_args then
            return nil, "--line-length requires an argument"
         end
         local n, err = parse_positive_integer(script_args[i], "--line-length")
         if err then
            return nil, err
         end
         apply_to.max_line_width = n
      elseif value == "-" then
         apply_to.read_stdin = true
      elseif value:sub(1, 2) == "--" then
         return nil, string.format("unknown flag: '%s'", value)
      else
         table.insert(apply_to.files, value)
      end
      i = i + 1
   end

   if apply_to.read_stdin and #apply_to.files > 0 then
      return nil, "cannot combine '-' (stdin) with file paths"
   end

   if #apply_to.files == 0 then
      table.insert(apply_to.files, ".")
   end

   local expanded, expand_err = file_discovery.expand_paths(apply_to.files)
   if expand_err then
      return nil, expand_err
   end
   apply_to.files = expanded

   return apply_to, nil
end

function options.from_config_file()
   local chunk, _load_err = loadfile("tlconfig.lua")
   if not chunk then
      return nil, nil
   end
   local config = chunk()
   if not (type(config) == "table") then
      return nil, "tlconfig.lua must return a table"
   end
   local formatter = config["cerulean"]
   if not formatter then
      return nil, nil
   end
   if not (type(formatter) == "table") then
      return nil, "tlconfig.lua: 'cerulean' must be a table"
   end
   local opts = options.default()

   local iw = formatter["indent_width"]
   if iw ~= nil then
      if not (type(iw) == "number") then
         return nil, "tlconfig.lua: cerulean.indent_width must be a positive integer"
      end
      local n = math.tointeger(iw)
      if not n or n <= 0 then
         return nil, "tlconfig.lua: cerulean.indent_width must be a positive integer"
      end
      opts.indent_width = n
   end

   local mlw = formatter["max_line_width"]
   if mlw ~= nil then
      if not (type(mlw) == "number") then
         return nil, "tlconfig.lua: cerulean.max_line_width must be a positive integer"
      end
      local n = math.tointeger(mlw)
      if not n or n <= 0 then
         return nil, "tlconfig.lua: cerulean.max_line_width must be a positive integer"
      end
      opts.max_line_width = n
   end

   local sr = formatter["sort_requires"]
   if sr ~= nil then
      if not (type(sr) == "boolean") then
         return nil, "tlconfig.lua: cerulean.sort_requires must be a boolean"
      end
      opts.sort_requires = sr
   end

   return opts, nil
end

function options.from_config_file_and_args(
   script_args)

   local file_opts, config_err = options.from_config_file()
   if config_err then
      return nil, config_err
   end

   local opts, parse_err = apply_args(file_opts or options.default(), script_args)
   if parse_err then
      return nil, parse_err
   end

   return opts, nil
end

return options
