-- default  lua require can't handle yielding across "require" calls
-- This version is implemented in pure-lua and avoids the problem
-- override the require function for everybody
-- this version is required for darktable.collection to function as a table

local orig_ipairs = ipairs
local function ipairs_iterator(st, var)
  var = var + 1
  local val = st[var]
  if val ~= nil then
    return var, st[var]
  end
end

ipairs = function(t)
  if getmetatable(t) ~= nil then -- t has metatable
    return ipairs_iterator, t, 0
  else
    return orig_ipairs(t)
  end
end

-- global flag indicating darktable has completed gui initialization and it is safe 
-- to register a lib in lighttable view
-- fixes issue #19197

darktable_gui_safe = false

local dt = require "darktable"


-- catch the view changed event from none to lighttable to ensure that
-- darktable gui initialization is complete

dt.register_event(
  "luarc_initialization", "view-changed",
  function(event, old_view, new_view)
    ov = nil
    if not old_view then
      ov = "none"
    else
      ov = old_view.id
    end
    if ov == "none" and new_view.id == "lighttable" then
      darktable_gui_safe = true
    end
  end
)

-- check for gui so that we don't hang darktable-cli

if dt.configuration.has_gui then
  require "tools/script_manager"
end

-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua
